将 DataContext 绑定到资源
Bind DataContext to Resource
我正在尝试将 DataContext 绑定到资源中的某个元素。这些资源嵌入到通过 DataTemplate 创建的 ContentControl 中,并且已经设置了 DataContext。这个 DataContext 有一个 StatusController(属性 名称:Status)我想附加到 StatusControllerViewModel,它将属性和事件从 StatusController 转换为 View 符合属性:
<ContentControl.Resources>
<CSharp:StatusControllerViewModel DataContext="{Binding Status}" x:Key="StatusViewModel"/>
</ContentControl.Resources>
问题是出现了这条信息:
Cannot find governing FrameworkElement or FrameworkContentElement for
target element
StatusControllerViewModel 派生自 DependencyObject,并有一个名为 DataContext 的 DependencyProperty。
当从 Freezable 派生它时它起作用了,为什么?
你的问题的原因取决于 WPF 不知道 FrameworkElement
使用什么来获取 DataContext,因为你的 StatusControllerViewModel
不属于 ContentControl 的可视化或逻辑树(因为它是一种资源)。
如果您使 StatusControllerViewModel
派生自 Freezable
- 如您所说 - 您就解决了问题,因为 Freezable 对象即使不在可视化或逻辑树中也可以继承 DataContext。
您可以找到有关 Freezable DataContext 继承的完整解释 here:
Normally, [...] DataContext bindings are resolved based on the target
dependency object's position within the element tree (or the namescope
to which the target dependency object belongs). But in this case, the
target dependency object is not actually in the tree. Instead, it is
just a property value on another object. That other object may or may
not be in the tree.
The reason the Freezable trick works is because a Freezable object has
its own notion of “inheritance context”. When the property engine
sets the effective value of a dependency property, it looks at that
new value to determine whether it is a dependency object that would
like to be part of a special inheritance tree.
希望对您有所帮助
我正在尝试将 DataContext 绑定到资源中的某个元素。这些资源嵌入到通过 DataTemplate 创建的 ContentControl 中,并且已经设置了 DataContext。这个 DataContext 有一个 StatusController(属性 名称:Status)我想附加到 StatusControllerViewModel,它将属性和事件从 StatusController 转换为 View 符合属性:
<ContentControl.Resources>
<CSharp:StatusControllerViewModel DataContext="{Binding Status}" x:Key="StatusViewModel"/>
</ContentControl.Resources>
问题是出现了这条信息:
Cannot find governing FrameworkElement or FrameworkContentElement for target element
StatusControllerViewModel 派生自 DependencyObject,并有一个名为 DataContext 的 DependencyProperty。
当从 Freezable 派生它时它起作用了,为什么?
你的问题的原因取决于 WPF 不知道 FrameworkElement
使用什么来获取 DataContext,因为你的 StatusControllerViewModel
不属于 ContentControl 的可视化或逻辑树(因为它是一种资源)。
如果您使 StatusControllerViewModel
派生自 Freezable
- 如您所说 - 您就解决了问题,因为 Freezable 对象即使不在可视化或逻辑树中也可以继承 DataContext。
您可以找到有关 Freezable DataContext 继承的完整解释 here:
Normally, [...] DataContext bindings are resolved based on the target dependency object's position within the element tree (or the namescope to which the target dependency object belongs). But in this case, the target dependency object is not actually in the tree. Instead, it is just a property value on another object. That other object may or may not be in the tree.
The reason the Freezable trick works is because a Freezable object has its own notion of “inheritance context”. When the property engine sets the effective value of a dependency property, it looks at that new value to determine whether it is a dependency object that would like to be part of a special inheritance tree.
希望对您有所帮助