I am currently playing with WPF data binding, and I came up with a problem that I do not understand. So I post the problem here, maybe you have an idea about what is spelled incorrectly.
First: I work with Visual Studio 2008 under Windows Vista 32bit, the problem is also present in Windows 7 RC1 64bit, the latest updates / service packs are installed except Vista, and still work with SP1.
Here is the problem: I cannot set the ValueConverter in the inherited Binding class.
Here is my own binding class:
public class MyBinding : Binding { public MyBinding() : base() { } public MyBinding(string path) : base(path) { } }
This class should do the same as the original Binding class, because it currently does not implement any native logic. I can use this class in XAML as follows:
<TextBlock Text="{local:MyBinding SomeProperty}" />
local is the namespace in which the MyBinding class is implemented.
Now here is the first thing I do not understand. VS2008 shows the following error message in its error window (the original message is in German because I use the German system - I do not have an error message in English, so I will try to translate)
Kein Konstruktor des MyBinding-Typs weist 1-Parameter auf.
(No constructor of type MyBinding takes 1 argument)
Althoug this error shows that the project compiles just fine and the MyBinding class is working properly. Why doesn't Visual Studio find the appropriate constructor (which, I would say, is correctly implemented)?
I can prevent this message if I change the XAML code to this:
<TextBlock Text="{local:MyBinding Path=SomeProperty}" />
The error message disappeared because the default constructor MyBinding is called, everything works fine, fine ...
Now I would like to set the ValueConverter for my property binding, the XAML looks like this:
<Window.Resources> <local:MyValueConverter x:Key="converter" /> </Window.Resources> [...] <TextBlock Text="{local:MyBinding Path=SomeProperty, Converter={StaticResource converter}}" /> [...]
... and here I get the following compilation error (original in German, and I think I also found the original message in English):
Beim Analysieren einer Markup extension wurde für den Typ "MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension" die unbekannte Eigenschaft "Converter" gefunden. Zeile X Position Y.
(Unknown property "Converter" for type "MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension" encountered when parsing the markup extension. Line x position Y)
Now I can no longer compile, because VS does not find the converter property (this is a public property of the original Binding class).
I managed to get the MyBinding class to work with the converter I specified, but only with a little hack: I added the following property to the MyBinding class:
public Type ConverterType { get { return Converter == null ? null : Converter.GetType(); } set { Converter = value == null ? null : (IValueConverter)Activator.CreateInstance(value); } }
... and XAML changes to this:
<TextBlock Text="{local:MyBinding Path=SomeString, ConverterType=local:MyValueConverter}" />
Now my project compiles and works fine. Actually, I think this is a good solution, because you do not need to specify the converter as a static resource, and the binding expression looks a little more clear to me. But in the end, this cannot be a solution.
So can someone tell me what I did wrong? Why can't I set the Converter property in my custom Binding class?
Thanks! Best regards, Renee