开发者

When to use DataTrigger and when to use Converter?

Any Assumption or rule is there, when we can use DataTrigger and when we can use Converter?开发者_如何学Go


DataTriggers are used to change styling depending on a specific value. For example change the TextBlock to red when its content is a number below 0.

Converter is used to convert a value to another type of value. For example to convert a boolean to a Visibility enum value


A DataTrigger can only be used to check for a specific value - it cannot e.g. check for ranges. A DataTrigger can be combined for multiple conditions (MultiTrigger) - a ValueConverter can only convert one value to another. (Of course, a MultiValueConverter can take many values (and listen for updates on all of them - but still only return one value.))

The two can be used in conjunction, though - say, having a ValueConverter that checks if the value is below 50 (returning true) to activate the DataTrigger (put it in the Binding of the DataTrigger).

So, there is no golden rule in my book - only circumstances that might make one of them unusable (or very, very hard to apply).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜