DatePicker的绑定设置默认值1/1/0001 WPF C# [英] DatePicker databinding sets default value to 1/1/0001 WPF c#
问题描述
我有一个对象中绑定到DATATIME一个datepicker,并在运行时,而不是字符串选择日期中的DatePicker,1/1/0001表示,很难用实际日历。我没有当我用细绳捆绑的DatePicker问题,但将其更改为一个DateTime这样做。请让我知道如何工作的。这是我到目前为止有:
在XAML我有:
<的DatePicker Grid.Column =4Grid.Row =9NAME =dueDateBoxVerticalAlignment =中心>
< DatePicker.SelectedDate>
<绑定路径=交货期UpdateSourceTrigger =的PropertyChanged模式=双向>
< Binding.ValidationRules>
<我:DateRangeRule />
< /Binding.ValidationRules>
< /绑定>
< /DatePicker.SelectedDate>
< / DatePicker的>
在C#中我有:
公众的DateTime DUEDATE
{
{返回的dueDate; }
组
{
如果(DUEDATE!= NULL)|| !DueDate.Equals(值))
{
的dueDate =价值;
OnPropertyChanged(交货期);
}
}
}
我也有一个依赖属性集,不知道这是否是扮演一个角色:
公共静态只读的DependencyProperty DueDateProperty =
DependencyProperty.Register(交货期的typeof(DateTime的),
typeof运算(UpdateJobDialog),新PropertyMetadata(DateTime.Now));
可能会帮助使用日期时间?而不是日期时间,日期时间以来是不是空的,你设置的1/1/0001默认值,而不是零,这将导致该请你来约会的消息。
公众的DateTime? DUEDATE
{
//需要改变私有变量的类型以及
{返回的dueDate; }
组
{
如果(DUEDATE!= NULL)|| !DueDate.Equals(值))
{
的dueDate =价值;
OnPropertyChanged(交货期);
}
}
}
I have a DatePicker bound to a DataTime within an object, and at run time, instead of the string "Select a Date" in DatePicker, "1/1/0001" is shown, making it hard to use the actual calendar. I did not have a problem when I was binding DatePicker with a string, but changing it to a DateTime did this. Please let me know how this works. This is what I have so far:
In XAML I have:
<DatePicker Grid.Column="4" Grid.Row="9" Name="dueDateBox" VerticalAlignment="Center">
<DatePicker.SelectedDate>
<Binding Path="DueDate" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<my:DateRangeRule/>
</Binding.ValidationRules>
</Binding>
</DatePicker.SelectedDate>
</DatePicker>
In the C# I have:
public DateTime DueDate
{
get { return dueDate; }
set
{
if (DueDate != null) || !DueDate.Equals(value))
{
dueDate = value;
OnPropertyChanged("DueDate");
}
}
}
I also have a dependency property set, not sure if this is playing a part:
public static readonly DependencyProperty DueDateProperty =
DependencyProperty.Register("DueDate", typeof(DateTime),
typeof(UpdateJobDialog), new PropertyMetadata(DateTime.Now));
Would probably help to use DateTime? instead of DateTime, since DateTime is not nullable, you are setting to the default value of 1/1/0001, rather than null which would result in the please select a date message you are used to.
public DateTime? DueDate
{
//Need to change the type of the private variable as well
get { return dueDate; }
set
{
if (DueDate != null) || !DueDate.Equals(value))
{
dueDate = value;
OnPropertyChanged("DueDate");
}
}
}
这篇关于DatePicker的绑定设置默认值1/1/0001 WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!