我创建了一个 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
}