在WPF数据网格中将Null或默认DateTime值显示为"空白"或NA

Dbl*_*oom 3 c# wpf datetime 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*"/>
Run Code Online (Sandbox Code Playgroud)

如果日期为"1/1/1753 12:00:00 AM"或"1/1/0001 12:00,如何应用带有datatemplate的样式或可显示" - "或"NA"的东西:00:00"?

Ran*_*ang 5

使用转换器,例如:

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;
}
Run Code Online (Sandbox Code Playgroud)

您应该设置最小/默认DateTime值或列表并与DateTime列进行比较,返回您想要的结果.