我在WPF应用程序中使用来自wpfToolkit.extended的Time Picker。
当我在dropdrownlist中选择一个时间时,一切都按照它应该的方式工作。这是工具包的命名空间。
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
这是timepicker的xaml
<Controls:MetroWindow
x:Class="TimeSheet.DayView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:e="http://schemas.microsoft.com/developer/msbuild/2003"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:TimeSheet.Views.DataTemplateSpace"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
x:Name="MainWin"
Title="DayView" Width="596" Height="596">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- <Style x:Key="ColoringStyle" TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
<Style.Triggers>
<DataTrigger Binding="{Binding isSynced}" Value="true">
<Setter Property="Background" Value="Gold"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding isSynced}" Value="false">
<Setter Property="Background" Value="Aqua"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>-->
<!-- Default DataTemplate -->
<DataTemplate x:Key="DefaultDataTemplate" >
<StackPanel Orientation="Horizontal" Width="596">
<TextBlock Text="{Binding ClientNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="145"/>
<TextBlock Text="{Binding ApplicationNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="90"/>
<TextBlock Text="{Binding StartTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="100"/>
<TextBlock Text="{Binding StopTimeBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="60"/>
<TextBlock Text="{Binding TaskNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="71"/>
<TextBlock Text="{Binding ProjectNameBinding}" Background="Transparent" Padding="0" Margin="0" TextWrapping="Wrap" Width="130"/>
</StackPanel>
</DataTemplate>
<!-- Editable DataTemplate -->
<DataTemplate x:Key="EditableDataTemplate">
<StackPanel Orientation="Horizontal" Width="596">
<!--<ComboBox x:Name="ClientComboBox" SelectionChanged="ClientComboBoxChanged" ItemsSource="{Binding Path=clientList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ClientNameBindingClass, Mode=OneWayToSource}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="145"/>-->
<TextBox Text="{Binding ClientNameBinding,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="145"/>
<TextBox Text="{Binding ApplicationNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="90"/>
<xctk:TimePicker Name="StartPicker" TextBoxBase.TextChanged="StartPicker_TextChanged" Value="{Binding StartValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
<xctk:TimePicker Name="EndPicker" TextBoxBase.TextChanged="EndPicker_TextChanged" Value="{Binding EndValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>
<TextBox Text="{Binding TaskNameBinding}" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" TextWrapping="Wrap" Width="71"/>
<ComboBox x:Name="ProjectComboBox" ItemsSource="{Binding Path=projectList, ElementName=MainWin}" SelectedValuePath="_id" DisplayMemberPath="_name" SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" Width="130" Background="Yellow" BorderThickness="0"/>
</StackPanel>
</DataTemplate>
<!-- DataTemplate Selector -->
<l:DayViewListDataTemplateSelector x:Key="templateSelector"
DefaultDataTemplate="{StaticResource DefaultDataTemplate}"
EditableDataTemplate="{StaticResource EditableDataTemplate}"/>
</ResourceDictionary>
</Window.Resources>
<Viewbox Stretch="Fill" Width="{Binding Width, ElementName=MainWin}" Height="{Binding Height, ElementName=MainWin}">
<Grid Margin="0,0,0,-4" Background="#FF3399FF" Height="557">
<Button x:Name="BackButton" Style="{DynamicResource MetroCircleButtonStyle}" Content="<---" Foreground="Blue" Click="BackButton_Click" Margin="7,2,524,491"/>
<Button x:Name="TimerButton" Width="124" Height="46" Click="TimerButton_Click" Margin="172,12,300,499">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="#FF5B6CB8"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Label x:Name="monthLabel" Content="May" RenderTransformOrigin="2.34,0.545" FontFamily="Times New Roman" FontSize="20" Margin="431,2,90,522"/>
<Label x:Name="dayDateLabel" Content="Friday, 27" RenderTransformOrigin="0.446,0.226" FontFamily="Times New Roman" FontSize="36" FontWeight="Bold" Margin="376,35,38,467"/>
<StackPanel Orientation="Horizontal" Margin="-1,93,1,434" RenderTransformOrigin="1.155,0.47" Height="25">
<TextBox Text="Client" HorizontalContentAlignment="Center" Width="145" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Application" HorizontalContentAlignment="Center" Width="90" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="StartTime" HorizontalContentAlignment="Center" Canvas.Left="148" Canvas.Top="86" Width="100" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" RenderTransformOrigin="0.5,0.5" VerticalContentAlignment="Center"/>
<TextBox Text="StopTime" HorizontalContentAlignment="Center" Width="60" RenderTransformOrigin="0.471,0.692" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Task" HorizontalContentAlignment="Center" Canvas.Left="378" Canvas.Top="86" Width="130" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
<TextBox Text="Project" HorizontalContentAlignment="Center" Width="71" Foreground="White" Background="Blue" Padding="0" BorderThickness="0" VerticalContentAlignment="Center"/>
</StackPanel>
<ListBox x:Name="listBox1" ItemsSource="{Binding}" Margin="0,131,0,59" ItemTemplateSelector="{StaticResource templateSelector}" SelectionMode="Single">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="MouseDoubleClick" Handler="listBox1_MouseDoubleClick">
</EventSetter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button x:Name="SyncEntry" Content="Sync All" HorizontalAlignment="Left" Margin="476,498,0,0" VerticalAlignment="Top" Width="95" Click="SyncEntry_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="#FF5256AC"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="EditButton" Content="Edit" HorizontalAlignment="Left" Margin="393,498,0,0" VerticalAlignment="Top" Width="75" Click="EditButton_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="#FF5256AC"/>
</LinearGradientBrush>
</Button.Background>
</Button>
<Button x:Name="CustomButton" Content="Add" HorizontalAlignment="Left" Margin="0,70,0,0" VerticalAlignment="Top" Width="75" FontSize="12" Height="20" FontWeight="Normal" Click="CustomButton_Click"/>
</Grid>
</Viewbox>
我已经看到了 here
的解决方案但是当我写C#代码 -
时public partial class DayView : MetroWindow, INotifyPropertyChanged
{
private DateTime currentDateForWindow;
public List<Harvest_Project> projectList{get;set;}
public List<Harvest_Client> clientList {get; set;}
public Harvest_Project selectedProjectid {get; set;}
public Harvest_Client selectedClientid {get; set;}
public string ElementName { get; set; }
public DateTime StartValue { get; set; }
public DateTime EndValue { get; set; }
public DayView(DateTime s)
{
InitializeComponent();
this.DataContext = this;
Globals._globalController.setDayViewWindow(this);
currentDateForWindow = s;
dayDateLabel.Content = s.DayOfWeek + ", " + s.Day;
monthLabel.Content = s.ToString("MMMM");
if(Globals._globalController.win32Manager.isTimerRunning)
TimerButton.Content = "Stop";
else
TimerButton.Content = "Start";
listBox1.Items.Clear();
listBox1.MouseDoubleClick += new MouseButtonEventHandler(listBox1_MouseDoubleClick);
clientList = Globals._globalController.harvestManager._CLIENTLIST;
projectList = Globals._globalController.harvestManager._PROJECTLIST;
Globals._globalController.fetchAndPopulateForDate(currentDateForWindow);
Globals.ni2 = new NotifyIcon();
Globals.ni2.Icon = TimeSheet.Properties.Resources.DayViewIcon;
Globals.ni2.Visible = true;
Globals.ni2.Click +=
delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == System.Windows.WindowState.Minimized)
{
this.Hide();
Globals.ni2.BalloonTipTitle = "DayView";
Globals.ni2.BalloonTipText = "This is Day window";
Globals.ni2.Visible = true;
Globals.ni2.ShowBalloonTip(500);
base.OnStateChanged(e);
}
}
public void addHarvestEntrytoView(Harvest_TimeSheetEntry entry)
{
listBox1.Items.Add(entry);
}
public void addHarvestEntrytoView(List<Harvest_TimeSheetEntry> entry)
{
foreach (Harvest_TimeSheetEntry x in entry)
listBox1.Items.Add(x);
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
Globals.ni2.Visible=false;
this.Hide();
Globals._globalController.getMonthViewWindow.Show();
Globals.ni1.Visible = true;
}
private void TimerButton_Click(object sender, RoutedEventArgs e)
{
if (!Globals._globalController.win32Manager.isTimerRunning)
{
Globals._globalController.win32Manager.startTimer();
this.TimerButton.Content = "Stop";
}
else
{
Globals._globalController.win32Manager.stopTimer();
this.TimerButton.Content = "Start";
}
}
private void SyncEntry_Click(object sender, RoutedEventArgs e)
{
//Submit All unsynced Entries
foreach(Harvest_TimeSheetEntry item in listBox1.Items)
{
if (!item.isSynced)
{
if (item.ProjectNameBinding == "Select Project" && item.ClientNameBinding == "Select Client")
System.Windows.MessageBox.Show("Please select you Project and Client");
else
//Check if something is selected in selectedProjectItem For that item
Globals._globalController.harvestManager.postHarvestEntry(item);
System.Windows.MessageBox.Show("Entry posted");
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Submit clicked Entry
if (sender is ListBoxItem)
{
ListBoxItem item = (ListBoxItem)sender;
Harvest_TimeSheetEntry entryToPost = (Harvest_TimeSheetEntry)item.DataContext;
if (!entryToPost.isSynced)
{
//Check if something is selected in selectedClientItem and selectedProjectItem For that items
if (entryToPost.ClientNameBinding == "Select Client" || entryToPost.ProjectNameBinding == "Select Project")
System.Windows.MessageBox.Show("Please select you Project and Client");
else
{
Globals._globalController.harvestManager.postHarvestEntry(entryToPost);
System.Windows.MessageBox.Show("Entry posted");
DataTemplate tmpl = (DataTemplate)this.FindResource("DefaultDataTemplate");
listBox1.ItemTemplate = tmpl;
}
}
else
{
//Already synced.. Make a noise or something
System.Windows.MessageBox.Show("Already Synced;TODO Play a Sound Instead");
}
}
}
private void EditButton_Click(object sender, RoutedEventArgs e)
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
if (item.isSynced)
{
item.isSynced = false;
listBox1.ItemTemplate = (DataTemplate)this.FindResource("EditableDataTemplate");
this.EditButton.Content = "Done Editing";
}
else
{
listBox1.ItemTemplate = (DataTemplate)this.FindResource("DefaultDataTemplate");
this.EditButton.Content = "Edit";
Globals._globalController.harvestManager.postHarvestEntry(item);
System.Windows.MessageBox.Show("Entry posted");
}
}
}
private void CustomButton_Click(object sender, RoutedEventArgs e)
{
Globals.ni2.Visible=false;
CustomView c = new CustomView(Globals._globalController.getDayViewWindow);
c.Show();
this.Hide();
Globals.ni3.Visible = true;
}
private void StartPicker_TextChanged(object sender, TextChangedEventArgs e)
{
StartPicker.Focus();
}
private void EndPicker_TextChanged(object sender, TextChangedEventArgs e)
{
EndPicker.Focus();
}
protected virtual void OnPropertyChanged(String time)
{
if (System.String.IsNullOrEmpty(time))
{
return;
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(time));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
它显示错误,因为“StartPicker和EndPicker在当前上下文中不存在”。
建议解决此错误的方法。
答案 0 :(得分:0)
的Xaml -
<Grid>
<xctk:TimePicker Name="StartPicker" TextBoxBase.TextChanged="StartPicker_TextChanged" Value="{Binding StartTimeBinding, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" Height="50" EndTime="11:59:0"/>
<xctk:TimePicker Name="EndPicker" TextBoxBase.TextChanged="EndPicker_TextChanged" Value="{Binding StopTimeBinding, ElementName=MainWin1, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" Height="50" EndTime="11:59:0"/>
</Grid>
Xaml.cs中Xaml背后的代码
private void EndPicker_TextChanged(object sender, TextChangedEventArgs e)
{
StartPicker.Focus(); // shows error at TimePicker Name(i.e StartPicker)
}
private void StartPicker_TextChanged(object sender, TextChangedEventArgs e)
{
EndPicker.Focus(); // shows error at EndPicker
}
答案 1 :(得分:0)
以这种方式尝试:
private void StartPicker_TextChanged(object sender, TextChangedEventArgs e)
{
TimePicker tp = sender as TimePicker;
if(tp != null)
{
tp.Focus();
}
}
private void EndPicker_TextChanged(object sender, TextChangedEventArgs e)
{
TimePicker tp = sender as TimePicker;
if(tp != null)
{
tp.Focus();
}
}
当然,你只能有一个功能,并且从时间选择器指向这个功能:
private void TimePicker_TextChanged(object sender, TextChangedEventArgs e)
{
TimePicker tp = sender as TimePicker;
if(tp != null)
{
tp.Focus();
}
}
然后在xaml中:
<xctk:TimePicker Name="StartPicker" TextBoxBase.TextChanged="TimePicker_TextChanged" Value="{Binding StartValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
<xctk:TimePicker Name="EndPicker" TextBoxBase.TextChanged="TimePicker_TextChanged" Value="{Binding EndValue, ElementName=MainWin, UpdateSourceTrigger=PropertyChanged}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" EndTime="11:59:0"/>