Hi

In my previous post about WPF Binding Converters one of our readers (Thanks The Reddest) pointed our that a new converter instance is not created every time a call is made to the converter. I promised to test it again so here goes :)
I used the old converter code but added a static integer inside the converter to count the number of calls made to it. and then printed it out to the trace in the constructor.

Here is the code that was added to the converter:

   1: private static int Counter = 0;
   2: 
   3: public IntConverter()
   4: {
   5:     Counter++;
   6:     Trace.WriteLine(string.Format("This converter was Created {0} Times", Counter));
   7: }

 

If indeed the converter is created every time a call is made we should get several lines in the trace with an increasing number.

I also added a Button to the window that will change the datacontext and by that trigger a converter call by the Binding. I did not want to fill this article with all the lines from the trace but the only relevant line was the following:

This converter was Created 1 Times

Which means that Indeed the converter was created only once! and that makes that way of using converters the best way! No need for singletons and static classes! If you don’t remember I recommend reading the previous article and its comments.

You can download the revised source code from our freebies page. Grab our feed and get the password.

Enjoy

Amit

Tags :

8 Responses to “WPF Binding Converter Best Practices Continued”


  1. Pioter

    Said on January 15, 2009 :

    hi
    in scenerio when creating Binding with converter in preocedural code compiler require converter object… so u using new Converter(). This operation creates one conveter per binding… not good, so u need singletoned converter class (like in yours prev article)
    i supose, xaml parser have “templated singleton class” for conveters -> it creates one converter per app (as u notice in article)

    cheers
    Pioter

  2. marg

    Said on February 13, 2009 :

    Hi Pioter,

    Do you mean you’ll need a singleton when using a Dynamically-generated Template with a binding and binding converter? I’m just about to do that and I was thinking the same thing.

  3. Miral

    Said on February 18, 2009 :

    Where did you use the converter?

    Because my expectation is that if you specify the converter in the resources for a Window, then it will be created whenever that Window is created (which might be multiple times, for a dialog window). If you specify it in the resources for a UserControl, then it will be created whenever the UserControl is created (which could be a great many times).

    I haven’t actually tested this expectation, though :)

    Specifying the converter in the application resources would get around this, though, if it is indeed the case.

  4. Miral

    Said on February 18, 2009 :

    Incidentally, I think you have an off-by-one bug in your blog code. When I posted my previous comment, it showed only one comment; now it’s showing two comments, and the second one wasn’t mine and was posted several days ago. And it’s not showing my comment, which should now be the third.

    I’m expecting that once I post this comment, my first comment will appear :)

  5. Daniel Schroeder

    Said on July 17, 2011 :

    So which way is the best way then? In this post you say “No need for singletons and static classes!”, so is Method 1 (The Static Resource Way) from your previous article the best method? In that article you mentioned that using that method was slower. I’m confused. Which method is the best?

  6. Ajay Gandhi

    Said on March 17, 2012 :

    In case same converter is used by multiple views, then multiple instance will be created.
    To avoid multiple instance creation, we can use singleton converter class.

2 Trackback(s)

  1. Apr 17, 2009: Dev102.com March 2009 Roundup | Dev102.com
  2. May 25, 2009: Creating Gapped and Bulleted Shapes in WPF/Silverlight | Dev102.com

Post a Comment