自定义绑定类无法正常工作 [英] Custom binding class is not working correctly

查看:194
本文介绍了自定义绑定类无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在玩WPF数据绑定,我遇到了一个我不明白的问题。所以我发布问题在这里,也许你有和想法什么地理错误。



起初:我正在使用Visual Studio 2008在Windows Vista 32位,问题是也存在于Windows 7 RC1 64bit,除了Vista之外,最新的更新/服务包已经安装,它仍然运行SP1。



这是问题:
I' m不能在继承的Binding类中设置ValueConverter。



这是我的自定义绑定类:

  public class MyBinding:Binding 
{
public MyBinding():base(){}
public MyBinding(string path):base(path){}
}

此类应该与原始的Binding类完全相同,因为目前它没有实现任何自己的逻辑。我可以在XAML中使用这个类,如下所示:

 < TextBlock Text ={local:MyBinding SomeProperty}/> 

local是实现MyBinding类的命名空间。



现在这里是我不明白的第一件事。 VS2008在其错误窗口中显示以下错误消息(原始消息是德文,因为我正在运行一个德文系统 - 我没有英文错误消息,所以我会尝试翻译)


Kein Konstruktor des MyBinding-Typs weist 1-Parameter auf。



(MyBinding类型的构造函数不需要1个参数)


这个错误显示项目编译很好,MyBinding类正常工作。为什么Visual Studio没有找到相应的构造函数(我会说,是否正确实现)?



如果我将XAML代码更改为此,我可以防止这些消息:

 < TextBlock Text ={local:MyBinding Path = SomeProperty}/> 

错误消息已经消失,因为MyBinding的默认构造函数被调用,一切正常,好的...



现在我想设置一个ValueConverter到我的属性绑定,XAML看起来像这样:

 < Window.Resources> 
< local:MyValueConverter x:Key =converter/>
< /Window.Resources>
[...]
< TextBlock Text ={local:MyBinding Path = SomeProperty,Converter = {StaticResource converter}}/>
[...]

...,这里我收到以下错误编译(原文为德文,我想,我也发现英文原文):


Beim Analysieren einer Markup Extension wurdefür den TypMS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtensiondie unbekannte EigenschaftConvertergefunden。 Zeile X位置Y。



(解析标记扩展时遇到的类型为MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension的未知属性转换器行x位置Y )


现在我不能编译,因为VS没有找到转换器属性(这是原来的Binding类的公共属性)。



我已经设法让MyBinding类使用我指定的转换器运行,但只有一点点黑客:
我添加了以下属性到MyBinding类:

  public Type ConverterType 
{
get {return Converter == null? null:Converter.GetType(); }
set {Converter = value == null? null:(IValueConverter)Activator.CreateInstance(value);
}

...和XAML更改为:

 < TextBlock Text ={local:MyBinding Path = SomeString,ConverterType = local:MyValueConverter}/> 

现在我的项目编译并运行正常。实际上,我认为它是一个很好的解决方案,因为你不必将转换器指定为静态资源,绑定表达式对我来说更清楚一些。但是最后,这不可能是解决方案。



所有人都可以告诉我我做错了什么?为什么我无法在自定义Binding类中设置Converter属性?



谢谢!
最好的问候,
René

解决方案

经过进一步的测试与标记扩展,这个问题通过了我的方式一遍又一遍,经过一些更多的谷歌搜索,我想我已经找到一个确认,这是Visual Studio设计器中的一个错误。所有有兴趣的人都应该看看



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



最佳问候,
René


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é

解决方案

after some further testing with markup extensions, this issue passed my way over and over again and after some more googling, I think I've found a confirmation, that this is a bug in Visual Studio designer. Everyone, whos interested in that should take a look at

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

Best regards, René

这篇关于自定义绑定类无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆