WPF XAML - DataTriggers或ValueConverters?最佳实践 [英] WPF XAML - DataTriggers or ValueConverters? Best practice
问题描述
我有一个的TextBlock
A窗口。这的TextBlock
已显示值R,如果绑定值为0或M,如果绑定值是1。
I have a Window with a TextBlock
. This TextBlock
has to show the value "R" if the binded value is 0 or "M" if the binded value is 1.
我有两个可能性:
ValueConverter方法
<TextBlock Binding="{Binding Path=Value, Converter={StaticResource valConverter}}"/>
其中, valConverter
是的IValueConverter
类,返回M或R如果值分别为0或1。
Where valConverter
is an IValueConverter
class that returns "M" or "R" if the value is respectively 0 or 1.
[omitted class]
DataTrigger方法
<TextBlock>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Value}" Value="0">
<Setter Property="TextBlock.Text" Value="R"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Value}" Value="1">
<Setter Property="TextBlock.Text" Value="M"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
据你,什么是最好的方法?
According to you, what is the best approach?
推荐答案
转换器在这种情况下最好的。正如其名称所示转换器将类型。在这种情况下,你要INT转换为字符,所以转换器多为宜。欲了解更多信息: ConverterPerformance
Converters are Best in this scenario. As the name indicates converter converts the type. In this case you want to convert int to Char, so converters are much suitable. For More Info : ConverterPerformance
这篇关于WPF XAML - DataTriggers或ValueConverters?最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!