Custom binding class is not working properly

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

+7
c # data-binding visual-studio wpf
source share
4 answers

after some additional testing with markup extensions, this problem went my way again and again, and after a few more searches, I think I found confirmation that this was a mistake in the Visual Studio designer. Everyone who is interested should take a look at

http://www.hardcodet.net/2008/04/nested-markup-extension-bug

Regards, Renee

+6
source share

Binding inheritance is probably not the best solution. If you just want to bypass the declaration of the converter as a static resource, try creating a Singleton of your converter and use it like this:

 Text="{Binding Path=Foo, Converter={x:Static local:MyConverter.Converter}}" 

Alternatively, you can try the markup extension as shown here .

+2
source share

Thanks for your reply!

In general, I have no problem with the binding expression syntax and declaration of the converter before using it. What I wrote above is just a good workaround for my main problem. I want to create my own Binding class and pass my own converter to it in the same way as with the original Binding class.

I just want to understand the error message that VS has presented to me. I think it should have a reason, either I'm doing something wrong, or there is an error in Visual Studio / WPF.

Until today, I was sure that the problem was with the computer. But I posted this question in two other user groups (also on the MSDN forum for WPF). So far, you are the only person who answers. And so I came to the conclusion that this could also be a problem with the visual studio ... I don’t know.

Again, thank you very much, I will look more closely at the page you posted (currently only short).

Have a nice weekend!

Regards, Renee

0
source share

I had the same problem, I don’t know why, but I put the user binding in another dll and it worked.

0
source share

All Articles