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.

I have two possibilities:

ValueConverter approach

<TextBlock Binding="{Binding Path=Value, Converter={StaticResource valConverter}}"/>

Where valConverter is an IValueConverter class that returns "M" or "R" if the value is respectively 0 or 1.

[omitted class]

DataTrigger approach

<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?

rPulvi
  • 946
  • 8
  • 33
  • 2
    possible duplicate of [WPF triggers VS Converter](http://stackoverflow.com/questions/19466354/wpf-triggers-vs-converter) – Anatolii Gabuza Jul 07 '14 at 10:05
  • possible duplicate of [Should I use WPF converter or trigger?](http://stackoverflow.com/questions/11152313/should-i-use-wpf-converter-or-trigger/11156189#11156189) – akjoshi Aug 18 '15 at 06:43

2 Answers2

4

In most of the scenarios triggers can perform the same work as any converter but Converters can have custom/business logic.

One limitation of Triggers is that Setters in your DataTriggers can only change properties of your UI elements; so, you can't update your ViewModels property with triggers, that's where Converters win, remember the ConvertBack method.

So in short Triggers can only perform OneWay operations whereas Converters can perform TwoWay operations

MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 8
    If you copy an answer from earlier question then pls. provide a link to original question, will be more helpful for everyone. http://stackoverflow.com/a/11156189/45382 – akjoshi May 18 '15 at 13:41
2

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

Sivasubramanian
  • 935
  • 7
  • 22