C# 为DataTemplate内的时间选择器设置日期和时间
在我的wpf应用程序中,我有两个位于DataTemplate内的timePicker控件 Xaml:C# 为DataTemplate内的时间选择器设置日期和时间,c#,wpf,datatemplate,timepicker,C#,Wpf,Datatemplate,Timepicker,在我的wpf应用程序中,我有两个位于DataTemplate内的timePicker控件 Xaml: 由于这些计时器位于DataTemplate内,因此在我的代码隐藏中无法访问。在这种情况下,有人能提出一些设置日期和时间的方法吗?我想我找到了解决方案,实际上这里发生的就是这个。您已将ListBox.ItemsSource属性设置为某个列表或集合,因此ListBoxItem Datacontext被设置为该列表,因此其EditableDataTemplate(包含时间选择器搜索)其绑定属性Star
由于这些计时器位于DataTemplate内,因此在我的代码隐藏中无法访问。在这种情况下,有人能提出一些设置日期和时间的方法吗?我想我找到了解决方案,实际上这里发生的就是这个。您已将ListBox.ItemsSource属性设置为某个列表或集合,因此ListBoxItem Datacontext被设置为该列表,因此其EditableDataTemplate(包含时间选择器搜索)其绑定属性StartValue和endValue位于列表或集合中,但实际上这些属性位于列表之外或页面中。因此,您必须如下设置TimePIcker的Relativesource属性
<DataTemplate x:Key="sample" >
<xctk:TimePicker Name="StartPicker" Value="{Binding DateTime, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
</DataTemplate>
如果希望所有项目的startTime和EndTime都不同,则创建一个包含startTime和EndTime属性的单独类。然后列出这些对象并将其绑定到您的列表框。希望您能收到此信息。对不起,我的英语不好。请使用属性数据绑定。检查谷歌。您的组合框是否正常。是的。ComboBox正在运行。更新您的问题并发布完整的xaml和代码隐藏代码。我认为这是一个非常小的问题。检查解决方案。告诉我它是否运行。我做了同样的事情。但时间选择器值未更新。是否已实现InotifyPropertyChangedInes。我已实现inotifypropertychanged。只需在集合上应用断点并获取部分属性,然后检查它是否运行正常。
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; }
private DateTime _dateTime1;
public DateTime StartValue
{
get
{
return _dateTime1;
}
set
{
_dateTime1 = value;
OnPropertyChanged("StartValue");
}
}
private DateTime _dateTime2;
public DateTime EndValue
{
get
{
return _dateTime2;
}
set
{
_dateTime2 = value;
OnPropertyChanged("EndValue");
}
}
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)
{
if (EditButton.Content == "Edit")
{
listBox1.ItemTemplate = (DataTemplate)this.FindResource("EditableDataTemplate");
EditButton.Content = "Done Editing";
}
else
{
foreach (Harvest_TimeSheetEntry item in listBox1.Items)
{
Globals._globalController.harvestManager.updateHarvestTimeSheetEntry(item);
if (!item.isSynced)
{
if (ValidateEntry(item))
if (IsValidTime(StartValue.ToString()) && IsValidTime(EndValue.ToString()))
Globals._globalController.harvestManager.postHarvestEntry(item);
System.Windows.MessageBox.Show("Entry posted");
}
}
EditButton.Content = "Edit";
listBox1.ItemTemplate = (DataTemplate)this.FindResource("DefaultDataTemplate");
}
}
private bool ValidateEntry(Harvest_TimeSheetEntry ent)
{
bool IsValid = true;
if (ent.ClientNameBinding == null || ent.TaskNameBinding == null || ent.ProjectNameBinding == null) throw new ArgumentNullException();
{
IsValid = false;
}
return IsValid;
}
public bool IsValidTime(string time)
{
Regex checktime =
new Regex(@"^(20|21|22|23|[01]d|d)(([:][0-5]d){1,2})$");
return checktime.IsMatch(time);
}
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;
}
public 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.Value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second);
EndPicker.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
<DataTemplate x:Key="sample" >
<xctk:TimePicker Name="StartPicker" Value="{Binding DateTime, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Format="Custom" FormatString="hh:mm tt" Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" EndTime="11:59:0"/>
</DataTemplate>