我应该使用WPF转换器或触发? [英] Should I use WPF converter or trigger?

查看:293
本文介绍了我应该使用WPF转换器或触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你是怎么决定何时使用转换,何时使用触发器。 我使用的是触发对GUI的操作(如显示/隐藏控件,改变它们的外观等)。

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

前段时间我用了一个 BooleanToVisibilityConverter 用于这一目的,但现在,我并不需要它,我连接到​​所有的事情能见度使用触发器,我甚至开始思考的什么是创造的目的 BooleanToVisibilityConverter 由MS队吗?的一般情况下,当它的可能,我尝试使用声明的方式来写的code - 在这个例子 - XAML。

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.

什么是你的这种看法?

推荐答案

我同意你的看法,我也尝试使用声明code在XAML和preFER 触发器转换器>

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

在大多数情况下触发器可以执行相同的工作,任何转换器,但转换可以有pchajer提到的定制/业务逻辑。

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

触发器的一个限制是,二传手在 DataTriggers 只能更改用户界面元素的属性的;因此,你不能用触发器,这就是更新您的ViewModels财产,其中转换赢,记得 ConvertBack 方法。

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.

因此​​,您可以将您的虚拟财产与控件绑定能见度使用 BooleanToVisibilityConverter 键,即使你的控制能见度已被其它装置来改变你的虚拟机属性将得到更新;通常它不是必需的,这就是为什么 BooleanToVisibilityConverter 会被替换成触发。

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

这篇关于我应该使用WPF转换器或触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆