WPF triggers VS Converter
Asked Answered
L

3

15

Which is better to use performance wise? Limitation of Converter compared to trigger and vice verse. Shall I limit use of converter because it can cause class explosion?

Latvina answered 19/10, 2013 at 13:9 Comment(0)
C
12

If you are into TDD development, it will be better choice to go with converters.

If there is no complex business logic or custom logic to be applied then go for Triggers. One more thing about the converters is that, there is a performance hit associated with using converters according to Laurent Bugnion (creator of MVVM Light).

You can see more insight on the performance hit from this post. DataTrigger vs databinding with converter performance wise

You can get more details on when to you use trigger and when to use converters from this post as well. Should I use WPF converter or trigger?

The decision can be taken based on the development approach you are following or going to follow. I prefer wrapping your view-model in a "WPF Viewmodel" concept most of the time.

Cathrinecathryn answered 19/10, 2013 at 14:59 Comment(0)
I
5

For similar type of output, Triggers are better performance wise as compared to Converter. Although, conveters are advisable where you want you development to be TDD as you can write unit tests for your converter code. Converters can be used to write complex conversions which sometimes not possible using Triggers. For complex conversions Converters can reduce the code rather than writing series of Triggers.

Ism answered 19/10, 2013 at 13:21 Comment(1)
Your answer is good but please add example like what complex conversion. It helps.Shroff
C
1

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

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

Chemarin answered 20/10, 2013 at 6:12 Comment(2)
it's funny how this answer is almost exactly the same as this otherSpecies
Wow, it has appeared again here: #24608452Wisla

© 2022 - 2024 — McMap. All rights reserved.