开发者

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

开发者 https://www.devze.com 2023-03-15 12:38 出处:网络
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 nul

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?

I开发者_运维问答s there any other XAML only solution?

Thank you in advance!


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>


There is no XAML only solution for this in Silverlight 4. In Silverlight 5 you could use a Markup Extension, though that's still code you would have to write. In Silverlight 4 you could use a Behavior as well.

My suggestion is to expose a property on your ViewModel which provides the null functionality. Then you wouldn't have to use a Converter, though the property you're binding to would work as you would like.

0

精彩评论

暂无评论...
验证码 换一张
取 消