如何在DateTime类型的属性中设置空白作为默认值
本文关键字:空白 设置 默认值 属性 DateTime 类型 | 更新日期: 2023-09-27 18:09:30
我已经创建了一个类型为DateTime的属性。当我在InvokePropertyChanged()事件的标签中设置此属性值时,它显示默认值(1/1/0001 12:00:00)。如果属性没有有效日期,我如何设置空白文本?
public event PropertyChangedEventHandler PropertyChanged;
private DateTime? _dDateTime = null;
public DateTime PostedDateTime
{
get
{
return _dDateTime.HasValue ? _dDateTime.Value : DateTime.Now;
}
set
{
_dDateTime = value;
InvokePropertyChanged(new PropertyChangedEventArgs("PostedDateTime"));
}
}
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
Refresh();
}
if (e.PropertyName.Equals("PostedDateTime"))
lblDateTimeDt.Text = PostedDateTime.ToString();// setting datetime in label
}
您可以在分配
之前测试值public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
Refresh();
}
if (e.PropertyName.Equals("PostedDateTime"))
lblDateTimeDt.Text = PostedDateTime == DateTime.MinValue ? "" : PostedDateTime.ToString();
}