在 WPF 数据网格中将 Null 或默认 DateTime 值显示为 "blank" 或 NA
Display a Null or default DateTime value as "blank" or NA in a WPF datagrid
我有一个绑定到自定义对象的 ObservableCollection 的数据网格。其中一列绑定到 DateTime 属性,并且许多对象具有最小 DateTime 值。
在我的 DataGrid 中,这是 DateTime 列的代码
<DataGridTextColumn Binding="{Binding Path=StartTime, StringFormat={}{0:MM/dd/yyyy hh:mm:ss tt}}" Header="Start Time" Foreground="Black" Width="2*"/>
如果日期是“1/1/1753 12:00:00 AM”或“1 /1/0001 12:00:00 AM"?
使用转换器,例如:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is DateTime && (DateTime)value < new DateTime(2, 1, 1))
{
return "NA";
}
else
return value;
}
您应该设置一个 minimum/default DateTime
值或一个列表并与 DateTime 列进行比较,return 您想要的结果。
我有一个绑定到自定义对象的 ObservableCollection 的数据网格。其中一列绑定到 DateTime 属性,并且许多对象具有最小 DateTime 值。
在我的 DataGrid 中,这是 DateTime 列的代码
<DataGridTextColumn Binding="{Binding Path=StartTime, StringFormat={}{0:MM/dd/yyyy hh:mm:ss tt}}" Header="Start Time" Foreground="Black" Width="2*"/>
如果日期是“1/1/1753 12:00:00 AM”或“1 /1/0001 12:00:00 AM"?
使用转换器,例如:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is DateTime && (DateTime)value < new DateTime(2, 1, 1))
{
return "NA";
}
else
return value;
}
您应该设置一个 minimum/default DateTime
值或一个列表并与 DateTime 列进行比较,return 您想要的结果。