Hi! Currently I'm playing around with WPF data binding and I came to an issue I dont understand. So I post the problem here, maybe you have and idea whats geoing wrong.
At first: I'm working with Visual Studio 2008 under Windows Vista 32bit, the problem is also present in Windows 7 RC1 64bit, latest updates/service packs are installed except Vista, its still running with SP1.
Here is the problem: I'm not able to set a ValueConverter in an inherited Binding class.
Here is my custom binding class:
public class MyBinding : Binding
{
public MyBinding() : base() { }
public MyBinding(string path) : base(path) { }
}
This class should do exactly the same as the original Binding class because currently it does not implement any own logic. I can use this class in XAML as follows:
<TextBlock Text="{local:MyBinding SomeProperty}" />
local is the namespace where the MyBinding class is implemented.
Now here comes the first thing I dont understand. VS2008 shows the following error message in its error window (the original message is in german, because I'm running a german system - i dont have the english error message, 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 is display the project compiles just fine and the MyBinding class is working as expected. Why Visual Studio does not find the corresponding constructor (wich, I would say, is properly implemented)?
I can prevent these message if I change the XAML code to this:
<TextBlock Text="{local:MyBinding Path=SomeProperty}" />
The error message is gone because the MyBinding's default constructor is called, everything works fine, ok...
Now I would like to set a ValueConverter to my property binding, 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 error while compiling (original in german and I think, I've 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 while parsing a Markup Extension. Line x position Y)
Now I cannot compile anymore because VS does not find the converter property (wich is a public property of the original Binding class).
I've managed to get the MyBinding class to run with a converter I specify, but only with a little hack: I've 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 runs fine. Actually I think, its a nice solution, because you dont have to specify the converter as a static resource and the binding expression looks a little bit more clearly to me. But at the end, this cannot be the solution.
So can anyone tell me what I've done wrong? Why I cannot set the Converter property in my custom Binding class?
Thank you! Best regards, René