Manoj Kawatra
Manoj Kawatra

Reputation: 151

WPF triggers VS Converter

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?

Upvotes: 15

Views: 5585

Answers (3)

Cornel Marian
Cornel Marian

Reputation: 2503

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

Upvotes: 1

Kishore Kumar
Kishore Kumar

Reputation: 21863

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.

Upvotes: 12

Nitin Purohit
Nitin Purohit

Reputation: 18580

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.

Upvotes: 5

Related Questions