自定义绑定类工作不正常自定义、不正常、绑定、工作

2023-09-03 16:21:01 作者:岢⒏岢苡鈈葽假裝堅強

目前我正在玩WPF数据绑定和我来到了一个问题,我不明白。所以,我在这里发表的问题,也许你已经和想法什么geoing错了。

首先:我与Visual Studio 2008在Windows Vista 32位工作时,问题也present在Windows 7 RC1的64位,最新的更新/服务包安装,除了Vista中,它仍与SP1运行

下面的问题: 我不能在继承的绑定类设置一个ValueConverter。

页面显示不正常,那些框架之类的东西和一些图片完全看不见 例如那些 提交 确定 按钮图片

下面是我的自定义绑定类:

 公共类MyBinding:绑定
{
    公共MyBinding():碱(){}
    公共MyBinding(字符串路径):基地(路径){}
}
 

本类应该做的完全一样,原来绑定类,因为目前它没有实现任何自己的逻辑。我可以在XAML中使用这个类,如下所示:

 < TextBlock的文本={地方:MyBinding SomeProperty}/>
 

地方是哪里MyBinding类实现的命名空间。

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

  

Kein Konstruktor德MyBinding-Typs weist 1参数AUF。

     

(类型MyBinding的任何构造函数1参数)

Althoug这个错误显示的项目编译就好及MyBinding类工作正常。为什么Visual Studio中没有找到相应的构造函数(至极,我会说,正确实施)?

我可以prevent这些消息,如果我改变了XAML code到这一点:

 < TextBlock的文本={地方:MyBinding路径= SomeProperty}/>
 

走了该错误消息,因为MyBinding的默认构造函数被调用时,一切正常,OK ......

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

 < Window.Resources>
    <地方:MyValueConverter X:关键=转换器/>
< /Window.Resources>
[...]
< TextBlock的文本={地方:MyBinding路径= SomeProperty,转换器= {的StaticResource器}}/>
[...]
 

......,在这里我得到以下错误而编译(原件德文,我想,我还发现原来用英语消息):

  

BEIM Analysieren einer标记扩展wurde献给巢穴典型值MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension死unbekannte Eigenschaft转换器gefunden。 Zeile X位置Ÿ。

     

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

现在我不能编译了,因为VS没有找到器属性(至极是原来的绑定类的公共属性)。

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

  public类型ConverterType
    {
        {返回转换器== NULL?空:Converter.GetType(); }
        集合{转换器=值== NULL?空:(的IValueConverter)Activator.CreateInstance(值); }
    }
 

...和XAML变为这样:

 < TextBlock的文本={地方:MyBinding路径= SomeString,ConverterType =本地:MyValueConverter}/>
 

现在我的项目编译并运行良好。其实我觉得,它是一个很好的解决方案,因为你没有指定的转换器,静态资源,并结合EX pression看起来多一点点清晰地浮现。但在最后,这不可能是该溶液中。

因此​​,谁能告诉我,我做了什么错?为什么我不能设置转换器属性在我的自定义绑定类?

感谢您! 诚挚的问候, 勒内·

解决方案

在一些进一步的测试用标记扩展,这个问题通过我的方式一遍又一遍,并经过一些google搜索,我想我已经找到了一个确认,该这是Visual Studio设计的错误。每个人,在卫生组织的兴趣应该看看

http://www.hard$c$ct.net/2008/04/nested-markup-extension-bug

最好的问候, 勒内·

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é