5

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


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.

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243

2 Answers2

4

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Can someone please clarify this answer. There's some typo somewhere which makes this very confusing. – NVM Feb 28 '11 at 09:43
  • "If you need unique converters: If you create a specific converter for a property, however, it will not be shared." This does not make any sense. There is something missing isn't it? Thanks. – NVM Feb 28 '11 at 14:09
  • 1
    @NVM: No. You can create an instnace of a converter for a specific dependency property. In this case, it won't be shared between other properties. If you use a resource, and reference the converter by key, it will be shared. – Reed Copsey Feb 28 '11 at 19:07
  • Thanks. I finally understood the sentence. English is not my native language :). I think it would be much clearer if you dropped the following "If you need unique converters:" – NVM Mar 01 '11 at 12:09
  • I have tried using multiple resources of the same converter type with different keys, but they all still are the same instance. It seems impossible to create different instances. – Neo Nov 04 '11 at 13:13
  • @Neo: You can define them inline, instead of in the resources. If you do, you should get separate instances... – Reed Copsey Nov 04 '11 at 16:13
2

Create a constructor and destructor in your converter and set breakpoints within to tell for sure. I just created a simple example and it looks like only one converter was created for my multiple viewmodels that were using the constructor

kenwarner
  • 28,650
  • 28
  • 130
  • 173
  • I also did this and had the same result--it became obvious that only one converter would be instantiated when I declared it as a single static resource in my XAML. – sourcenouveau Jan 14 '10 at 21:19