如何将DateTime的默认值设置为空字符串?
我有一个名为Raised_Time的属性,这个属性显示了在datagrid Cell中引发警报的时间。当用户创建任何警报时,我不想在datagrid单元格中显示任何内容,只显示空单元格。如何将DateTime的默认值设置为空字符串?
我在互联网上搜索并发现DateTime的默认值可以使用DateTime.MinValue进行设置,并且这将显示日期时间i的最小值:e“1/1/0001 12:00:00 AM”。
相反,我希望datagrid单元保持空白,直到发出警报时,它不显示任何时间。
我认为datatrigger可以写在这种情况下。我无法为此场景编写数据触发器。我是否还需要一个转换器来检查DateTime是否设置为DateTime.MinValue,使datagrid单元格保持空白?
请帮忙!!
如何只改变你的财产链接到的DateTime的私人领域如:
public string Raised_Time
{
get
{
if(fieldRaisedTime == DateTime.MinValue)
{
return string.Empty();
}
return DateTime.ToString();
}
set
{
fieldRaisedTime = DateTime.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
}
}
什么是价值来自一个实体框架创建的对象...从DB .... – Dani 2013-03-22 14:23:03
我用这个nullable datetime
,具有扩展方法,如:
public static string ToStringOrEmpty(this DateTime? dt, string format)
{
if (dt == null)
return string.Empty;
return dt.Value.ToString(format);
}
好点兄弟!谢谢,++++! ) – 2012-11-12 22:52:33
我看到两个简单的选项来解决这个问题:
您使用Nullable数据类型
DateTime?
,这样如果闹钟时间未设置,您可以存储null
而不是DateTime.MinValue
。您可以使用转换器,here is an example。
我会使用一个转换器,因为这是我可以很容易地看到在未来重用。这是我曾经使用过的一个DateFormat的字符串值作为ConverterParameter。
public class DateTimeFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((DateTime)value == DateTime.MinValue)
return string.Empty;
else
return ((DateTime)value).ToString((string)parameter);
}
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
在互联网上用谷歌搜索..不错;) – Arcturus 2010-09-07 12:37:30