它是更好地使用,而不是绑定DataTrigger? [英] Is it better to use DataTrigger instead of a databinding?
本文介绍了它是更好地使用,而不是绑定DataTrigger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想隐藏(或更改可见性,颜色等)在WPF元素是它更好地使用DataTrigger或转换器的绑定?
If I wanted to hide(or change visibility, color, etc) of an element in WPF is it better to use DataTrigger or a binding with a converter?
听起来像是两种方式来达到同样的目的。当它更好用户一个比另一个?
Sounds like two ways to achieve the same goal. When is it better to user one over another?
推荐答案
我可以告诉你我的经验。
I can tell you about my experience.
我使用数据绑定使用转换器以下情况:
I use databinding with converters for the following cases:
- 对于
能见度
属性(有内置的WPF转换器)。 - 在Silverlight应用程序(虽然有Silverlight的兼容在
Microsoft.Ex pression.Interactions
库数据触发,他们是不是很方便的WPF触发器) - 如果源对象包含许多可能的值。枚举到图像转换器,例如,它更容易编写5的if-else子句大于5 datatriggers。
- For the
Visibility
property (there is the built-in converter in WPF). - In Silverlight applications (although there are silverlight-compatible data triggers in the
Microsoft.Expression.Interactions
library, they are not as convenient as WPF triggers). - If the source object contains many possible values. Enum to image converter, for example, it is easier to write 5 if-else clauses than 5 datatriggers.
Datatriggers:
Datatriggers:
- 如果我想在一次(背景,知名度,厚度)发生改变几种不同的属性。
- 对于刷子(这是不容易通过颜色的十六进制数来创建C#code刷)。
- 如果我想申请静态资源。
- 它们可用于显示动画和运行故事板。
有时我preFER的MVVM的做法:我创建的视图模型所需类型的附加属性,使他们能够直接无需转换的约束
Sometimes I prefer the MVVM approach: I create additional properties of the necessary type in the viewmodel so that they can be bound directly without converting.
这篇关于它是更好地使用,而不是绑定DataTrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文