[英]DateTime XAML Binding String Format to show “null” for DateTime.MinValue
我對約會有約束力:
<TextBlock Text="{Binding Path=EndDateTime, StringFormat=d}"/>
我想要的是當它的值是DateTime.MinValue
(DateTime的默認值)時顯示null而不是日期。
這可能不使用轉換器,只是通過某種方式擴展我的綁定的StringFormat
屬性?
是否還有其他XAML解決方案?
先感謝您!
您可以在TextBlock的樣式中使用DataTrigger
<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>
在Silverlight 4中沒有針對此的XAML解決方案。在Silverlight 5中,您可以使用標記擴展,盡管這仍然是您必須編寫的代碼。 在Silverlight 4中,您也可以使用行為。
我的建議是在ViewModel上公開一個提供null功能的屬性。 然后你不必使用轉換器,雖然你綁定的屬性可以按你的意願工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.