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