是值转换器实例每个绑定在WPF? [英] Are value converters instantiated per-binding in WPF?

查看:193
本文介绍了是值转换器实例每个绑定在WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被实例化每一个绑定一个特定的值转换器类中使用?一个独立的WPF值转换器对象

Is a separate WPF value converter object instantiated for each binding that a particular value converter class is used in?

我想创建一个双向位到布尔值转换器。我希望能够在一个值类型布尔属性绑定(如器isChecked )以一个位(像 USHORT )。我使用转换器参数参数指定位。实施ConvertBack()方法很容易,但转换()是有点棘手。

I am trying to create a two-way bit-to-boolean value converter. I would like to be able to bind a bool property (such as IsChecked) to a bit in a value type (like a ushort). I'm using the converter's parameter arguments to specify the bit. Implementing the ConvertBack() method is easy, but Convert() is little trickier.

在转换()我需要知道什么价值的整个 USHORT 是这样我就可以切换只是单个位我很感兴趣,我想的只是在我的价值转换器类使用一个成员变量,用于临时存储这个每当ConvertBack()被调用,从而导致上述问题:是否每个结合得到自身的价值转换的实例

In Convert() I need to know what the value of the entire ushort is so I can toggle just the single bit I am interested in. I was thinking of just using a member variable in my value converter class to temporarily store this whenever ConvertBack() is called, thus leading to the above question: does each binding get its own value converter instance?

推荐答案

如果您在使用你的资源定义的转换器,它会被你之间共享的属性。

If you use a converter defined in your resources, it will be shared amongst your properties.

如果您需要独特的转换器:
如果为一个属性创建一个特定转换器,但是,它也不会被共享。无论哪种选择是可行的。甚至可以在资源使用多个转换器(同一类型的,具有不同的密钥),这将创造独特的实例

If you need unique converters: If you create a specific converter for a property, however, it will not be shared. Either option is workable. You can even use multiple converters (of the same type, with different keys) in your resources, which will create unique instances.

这篇关于是值转换器实例每个绑定在WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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