Should I use WPF converter or trigger?
Asked Answered
C

5

16

I was wondering how do you decide when to use converters and when to use triggers. I prefer using triggers for operations on GUI (like showing/hiding controls, changing their appearance etc.).

Some time ago I used a BooleanToVisibilityConverter for this purpose but now, I just don't need it, I do all things connected to the visibility using a triggers and I even started to think "what was the purpose of creating a BooleanToVisibilityConverter by the MS team?". Generally, when it's possible I try to use a declarative way to write the code - in this example - XAML.

What is your opinion of that?

Click answered 22/6, 2012 at 8:1 Comment(2)
trigger are used for single value check where converter used for complex and diffrent type of value conversion.Abominable
Similar question hinting towards performance cost of converters - #5508659Alexis
A
24

I agree with you, I also try to use declarative code in XAML and prefer Triggers instead of Converters.

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

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, thats where Converters win, remember the ConvertBack method.

So, you can bind your VM property with a controls Visibility using BooleanToVisibilityConverter and even if your controls visibility is changed by some other means your VM property will get updated; generally it's not required that's why BooleanToVisibilityConverter gets replaced by triggers.

So in short -

Triggers can only perform OneWay operations whereas Converters can perform TwoWay operations

Alexis answered 22/6, 2012 at 12:30 Comment(4)
it's funny how this answer is almost exactly the same as this otherBloater
@Bloater I would say 'how other answer is almost exactly same as this one' :D Thanks for pointing that out, made me smile :)Alexis
yes, to stay on the subject, I just copy-pasted my comment here and there :)Bloater
It's appeared again #24608452. This answer gets around.Oao
E
2

In my opinion, you are looking from bottom-up and you just need to look top-down.

Triggers-When a specified condition is fulfilled it "triggers" an execution

Converters-Convert between two incompatible types.

Why do we need a boolean datatype when we can do the same functionality with intergers?

Edgell answered 22/6, 2012 at 14:41 Comment(0)
P
1

You can achieve the functionality either by trigger or converter but from my option below possibility can be considered while taking decision

  1. If you are using TDD approach for development then go for converters as you can write test cases.
  2. If there is any business logic better to right code in converter and sometiems which can not be achieved through trigger.
Painter answered 22/6, 2012 at 8:17 Comment(0)
B
1

In addition to what has been said above I can only add:

  • Triggers sometimes require to duplicate things, e.g. when you have more than one property for triggers you need to specify each an every combination
  • Sometimes you will need code to properly convert from type A to B, then you have to use converters. Triggers are good if the value/property is already exposed from the VM in way, that you can use it for triggers.
Bioluminescence answered 22/6, 2012 at 12:37 Comment(0)
A
0

You should always do business related operations in the DomainModel objects, or at least in a ViewModel object. Making some business work in converter is not a good option because Converters are designed to convert a value from one type to another.

Accounting answered 22/3, 2013 at 14:28 Comment(1)
Dida you see my question? I explicitly wrote: "for operations onGUI (like showing/hiding controls, changing their appearance etc.)". I know that business related options must be controlled on the DomainModel object.Click

© 2022 - 2024 — McMap. All rights reserved.