1

I have a GridView with some elements binded to my object. The value where I'm working is this:

<GridViewColumn x:Name="DataGridLastEdit" Header="Last Edit" Width="150px" DisplayMemberBinding="{Binding lastEdit}" />

lastEdit is a DateTime type, but for some elements its empty and it display 1/1/0001 12:00:00 AM (it can change based on computer time format setting), making a pretty ugly effect.

I just want to hide it for element who don't have a valid date, but I'm pretty new to XAML and I can't to that. I read about using Converters but I always do some errors writing it and can't even try if it works, how can I do that?

4

1 回答 1

1

使用ValueConverter

public class EmptyDateCoverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(value is DateTime)
        {
            if((DateTime)value == DateTime.MinValue)
            {
                 return "";
            }
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //We don't need convert back
        throw new NotImplementedException();
    }
}

为转换器创建一个资源,例如在控件的资源中(conv是定义转换器的命名空间的别名)

<conv:EmptyDateCoverter x:Key="EmptyDate" />

然后以这种方式应用绑定:

<GridViewColumn x:Name="DataGridLastEdit" Header="Last Edit" Width="150px" DisplayMemberBinding="{Binding lastEdit, Converter={StaticResource EmptyDate}}" />
于 2014-01-15T08:30:35.677 回答