1

I have a column that is bound to a property of my object. The property, LastRunDate, is a Datetime. On default/start, it displays a date along the lines of of 1/1/1 12:00 AM.

Is there a way in my XAML to format my date where if I have a date of this value, to instead show a string of "None" or even a blank? Is there a way I can put this into my string format?

Here's the XAML right now:

       <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
            <DataGridTextColumn Binding="{Binding SourceServerName}" Header="Server Source" />
            <DataGridTextColumn Binding="{Binding SourceDataBaseName}" Header="Database" />
            <DataGridTextColumn Binding="{Binding LastRunTime, StringFormat={}\{0:MM/dd/yyyy hh:mm\}}" Header="Last Run Time" />
        </DataGrid.Columns>
1
  • 1
    If switching to a nullable DateTime? does not work for you, I would recommend an IValueConverter Commented Jul 28, 2015 at 21:23

2 Answers 2

3

Converter:

class DateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is DateTime)
        {
            var test = (DateTime)value;
            if (test == DateTime.MinValue)
            {
                return "None";
            }
            var date = test.ToString("MM/dd/yyyy hh:mm");
            return (date);
        }

        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<DataGrid ItemsSource="{Binding Items}">
    <DataGrid.Resources>
        <stackOverflow:DateTimeConverter x:Key="DateTimeConverter"/>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding LastRunTime, Converter={StaticResource DateTimeConverter}}" Header="Last Run Time" />
    </DataGrid.Columns>
</DataGrid>
Sign up to request clarification or add additional context in comments.

2 Comments

You may want to implement the ConvertBack method to parse "None" as DateTime.MinValue if you go this route so you don't attempt to store the string "None" in a DateTime object.
That would only be required if the DataGrid would be editable, allowing the UI to update the source property.
1

Change the type of LastRunDate to Datetime?. Then use this:

<DataGridTextColumn Binding="{Binding LastRunTime, TargetNullValue=None,
                     StringFormat={}\{0:MM/dd/yyyy hh:mm\}}" Header="Last Run Time" />

Since your property isn't initialized on start (i.e. it is null), you will see "None".

4 Comments

Thanks Hamlet. However, I tried it and still saw a string of the date. From my understanding (I could be wrong), DateTime isn't ever actually "null", but a default minvalue when created on new. LastRunTime is a DateTime type already.
Do you mean that you can't change the type of LastRunTime?
No I mean to say that it's always been the DateTime type.
Can you show your model and how do you initialize it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.