WPF triggers VS Converter

倖福魔咒の 提交于 2019-12-17 21:01:48

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/19466354/wpf-triggers-vs-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!