如何使UIElement支持绑定? [英] How can I make UIElement support binding?

查看:418
本文介绍了如何使UIElement支持绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DependencyProperties on UIElement s不支持数据绑定(您可以获得以下内容:

DependencyProperties on UIElements do not support databinding (you get something like:


找不到管理
FrameworkElement ..)

"Cannot find governing FrameworkElement..")

。如果你尝试,你会收到一个错误,因为WPF无法解析DataContext。从我所知道的,如果您继承了FrameworkElement或Freezable,您将获得有约束力的支持,但在这种情况下,我不能简单地更改基类。有没有办法让UIElement支持数据绑定?

. If you try, you get an error because WPF can not resolve the DataContext. From what I know, you get binding support if you inherit FrameworkElement or Freezable, but In this case I can not simply change the base class. Is there any way to get the UIElement to support data binding?

我试图将DataContext属性添加到UIElement类,如下所示:

I've tried to add the DataContext property to the UIElement class like this:

  FrameworkElement.DataContextProperty.AddOwner(typeof(Bitmap), new 
    FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));

我还尝试通过在绑定表达式中指定ElementName进行绑定,但是我仍然无法解决父DataContext(我认为通过ElementName显式绑定将简单地消除解析DataContext的需要)。

I also tried to bind by specifying "ElementName" in the binding expression, but I am still unable to resolve the parent DataContext (I thought that binding explicitly by ElementName would simply remove the need to resolve the DataContext).

这是绑定。该类被称为Bitmap。

This is the binding. The class in question is called "Bitmap".

<Utils:Bitmap Source="{Binding Path=Icon}" />
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" MaxWidth="90" Text="{Binding Path=Name}" TextWrapping="Wrap" TextAlignment="Center"/>

文本框绑定按预期工作,第一个绑定没有。绑定的viewmodel有两个属性(我绑定到Image类之前和它的工作)。

The textblock binding works as expected, the first binding does not. The bound viewmodel has both properties (I bound to the Image class before and it worked).

位图类可以在这个博客中找到: http://blogs.msdn.com/b/dwayneneed/archive/2007 /10/05/blurry-bitmaps.aspx

The bitmap class can be found at this blog: http://blogs.msdn.com/b/dwayneneed/archive/2007/10/05/blurry-bitmaps.aspx

使用一些扩展绑定诊断程序,我得到以下输出:

With some extended binding diagnostics, I get this output:

System.Windows.Data Warning: 65 : BindingExpression (hash=14926099): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Icon; DataItem=null; target element is 'Bitmap' (HashCode=117163); target property is 'Source' (type 'BitmapSource')
System.Windows.Data Warning: 63 : BindingExpression (hash=6195855): Resolving source  (last chance)
System.Windows.Data Warning: 65 : BindingExpression (hash=6195855): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Icon; DataItem=null; target element is 'Bitmap' (HashCode=55762700); target property is 'Source' (type 'BitmapSource')
System.Windows.Data Warning: 63 : BindingExpression (hash=48657561): Resolving source  (last chance)
System.Windows.Data Warning: 65 : BindingExpression (hash=48657561): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Icon; DataItem=null; target element is 'Bitmap' (HashCode=35264868); target property is 'Source' (type 'BitmapSource')


推荐答案

您必须继承 FrameworkElement 才能使用数据绑定。如果你不能改变基类,你唯一的选项就是H.B.说,创建一个从 FrameworkElement 派生的适配器,并将所有功能委托给从 UIElement

You have to inherit FrameworkElement to use data binding. If you cannot change the base class, the only option you have is, as H.B. said, create an adapter that will be derived from FrameworkElement and will delegate all the functionality to an instance of the existing class derived from UIElement.

请参阅 http://msdn.microsoft.com/en-us/library/ms743618.aspx 有关基本框架类(如 UIElement FrameworkElement )提供。

See http://msdn.microsoft.com/en-us/library/ms743618.aspx for more information on what the base framework classes (like UIElement and FrameworkElement) provide.

更新:

即使MSDN(上面的链接)表示数据绑定支持是在 FrameworkElement 级别上引入的,可以在任何 DependencyObject 。唯一的情况是,在这种情况下,您不能使用 DataContext 作为绑定的隐式来源,您不能使用 ElementName 用于引用源。

Even though MSDN (link above) says that the data binding support is introduced on the FrameworkElement level, it IS possible to set binding on any DependencyObject. The only thing is that in this case you cannot use DataContext as an implicit source for the binding and you cannot use ElementName for referring to the source.

您可以做的是以编程方式设置绑定并明确指定源:

What you can do is to set binding programmatically and specify source explicitly:

BindingOperations.SetBinding(MyBitmap, Bitmap.IconProperty, new Binding() { Source = this.DataContext /* Or any other source */, Path = new PropertyPath("Icon")});

或者你可以使用一个小技巧,使用 RelativeSource 用于引用视觉树中的元素(在这种情况下,任何一个父 FrameworkElement ):

OR you can use a little trick and use RelativeSource for referring to an element in the visual tree (in this case any parent FrameworkElement):

<Utils:Bitmap Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type FrameworkElement}}, Path=DataContext.Icon}" />

这篇关于如何使UIElement支持绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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