Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime XAML Binding String Format to show "null" for DateTime.MinValue

I have a binding to a date:

<TextBlock Text="{Binding Path=EndDateTime, StringFormat=d}"/>

What I want is that when its value is DateTime.MinValue (DateTime's default value) to display null instead of the date.

Is this possible without using a converter, simply by somehow extending my binding's StringFormat property?

Is there any other XAML only solution?

Thank you in advance!

like image 582
Dummy01 Avatar asked Jun 27 '11 13:06

Dummy01


1 Answers

You could use a DataTrigger in the TextBlock's Style

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding Path=EndDateTime, StringFormat=d}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding EndDateTime}" Value="{x:Static sys:DateTime.MinValue}">
                    <Setter Property="Text" Value="NULL" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
like image 116
Rachel Avatar answered Oct 19 '22 13:10

Rachel