如何使UIElement支持绑定?

Mar*_*ius 3 data-binding wpf datacontext uielement

UIElements 上的DependencyProperties 不支持数据绑定(你得到类似的东西:

"找不到管理FrameworkElement ..")

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

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

  FrameworkElement.DataContextProperty.AddOwner(typeof(Bitmap), new 
    FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
Run Code Online (Sandbox Code Playgroud)

我还尝试通过在绑定表达式中指定"ElementName"来绑定,但我仍然无法解析父DataContext(我认为ElementName显式绑定只会消除解析DataContext的需要).

这是绑定.有问题的类称为"位图".

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

文本块绑定按预期工作,第一个绑定不工作.绑定的viewmodel具有两个属性(我之前绑定到Image类并且它工作).

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

通过一些扩展的绑定诊断,我得到了这个输出:

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')
Run Code Online (Sandbox Code Playgroud)

Pav*_*kov 7

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

有关基本框架类(如和)提供的内容的更多信息,请参见http://msdn.microsoft.com/en-us/library/ms743618.aspx.UIElementFrameworkElement

更新:

即使MSDN(上面的链接)说在数据FrameworkElement级别上引入了数据绑定支持,也可以在任何级别上设置绑定DependencyObject.唯一的问题是,在这种情况下,您不能DataContext用作绑定的隐式源,也不能ElementName用于引用源.

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

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

或者您可以使用一个小技巧并RelativeSource用于引用可视树中的元素(在本例中为任何父元素FrameworkElement):

<Utils:Bitmap Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type FrameworkElement}}, Path=DataContext.Icon}" />
Run Code Online (Sandbox Code Playgroud)


hai*_*imb 5

您可以使用{Binding Source={x:Reference elementName}}代替{Binding ElementName=elementName}.