wpf - 如何让 UIElement 支持绑定(bind)?

标签 wpf data-binding datacontext uielement

UIElement 上的 DependencyProperties 不支持数据绑定(bind)(您会得到类似以下内容:

"Cannot find governing FrameworkElement..")

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

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

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

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

这是绑定(bind)。有问题的类称为“Bitmap”。

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

文本 block 绑定(bind)按预期工作,而第一个绑定(bind)则不然。绑定(bind)的 View 模型具有这两个属性(我之前绑定(bind)到 Image 类并且它有效)。

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

通过一些扩展的绑定(bind)诊断,我得到以下输出:

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才能使用数据绑定(bind)。如果您无法更改基类,那么您唯一的选择就是,如 H.B.也就是说,创建一个从 FrameworkElement 派生的适配器,并将所有功能委托(delegate)给从 UIElement 派生的现有类的实例。

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

更新:

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

您可以做的是以编程方式设置绑定(bind)并显式指定源:

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

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

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

关于wpf - 如何让 UIElement 支持绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6071086/

相关文章:

c# - 如何在 WPF XAML 中将 ControlTemplate Enabled 属性绑定(bind)到不透明度

javascript - 具有深层次数据的 ReactJS 数据流

c# - Winforms DataGridView 数据绑定(bind)到复杂类型/嵌套属性

wpf - 如何将 POCO 对象绑定(bind)到 WPF GUI?

xaml - 设置 datacontext 时如何摆脱设计时 XAML 错误 "Object reference not set to an instance of object"

c# - WPF:向窗口添加用户控件

c# - MVVM Light - 多个 ViewModel(并将它们连接起来)

c# - 如何向 WPF TabControl 添加额外内容?

java - org.springframework.beans.NotReadablePropertyException : Invalid property. Bean属性不可读或具有无效的getter方法

c# - Entity Framework 线程安全