在UserControl中设置DataContext正在影响父项中的绑定 [英] Setting DataContext within UserControl is affecting bindings in parent
问题描述
我有一个基本的 UserControl
,将其 DataContext
设置为自身,以方便绑定:
I have a basic UserControl
that sets its DataContext
to itself for ease of binding:
<UserControl x:Class="MyControlLib.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
</UserControl>
这是在父XAML文件中使用的:
This is used in a parent XAML file like this:
<UserControl x:Class="MyControlLib.ParentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctrl="clr-namespace:MyControlLib">
<ctrl:ChildControl x:Name="ChildName"
PropertyOnChild="{Binding PropertyInParentContext}"/>
</UserControl>
由于某种原因,这给出了一个绑定错误,似乎表明父控件的DataContext
受到子控件设置自己的 DataContext
的影响。
For some reason, this gives a binding error that seems to indicate that the DataContext
of the parent control is getting affected by the child control setting its own DataContext
.
System.Windows.Data错误:40:BindingExpression路径错误:'PropertyControl'(Name ='ChildName')'上找不到'PropertyInParentContext'属性。 BindingExpression:Path = PropertyInParentContext; DataItem ='ChildControl'(Name ='ChildName');目标元素为'ChildControl'(Name ='ChildName'); target属性为'PropertyOnChild'(键入'whatever')
System.Windows.Data Error: 40 : BindingExpression path error: 'PropertyInParentContext' property not found on 'object' ''ChildControl' (Name='ChildName')'. BindingExpression:Path=PropertyInParentContext; DataItem='ChildControl' (Name='ChildName'); target element is 'ChildControl' (Name='ChildName'); target property is 'PropertyOnChild' (type 'whatever')
为什么PropertyInParentContext正在寻找子控件,而不是父母的 DataContext
?
Why is "PropertyInParentContext" being looking for in the child control rather than in the parent's DataContext
?
如果我删除
DataContext="{Binding RelativeSource={RelativeSource Self}}
从孩子控制,然后事情操作我的期望。
from the child control, then things operate how I would expect.
我在这里缺少一些明显的东西?
Am I missing something obvious here?
推荐答案
控制和实例化的声明基本上是操纵同一个对象,声明中设置的所有属性也在每个实例上设置,所以如果属性是可见的可以这么说:
The declaration of your control and the instantiation are basically manipulating the same object, all the properties that are set in the declaration are also set on every instance. So if the properties were "visible" so to speak:
<UserControl x:Class="MyControlLib.ParentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ctrl="clr-namespace:MyControlLib">
<ctrl:ChildControl x:Name="ChildName"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
PropertyOnChild="{Binding PropertyInParentContext}"/>
</UserControl>
这就是为什么你不设置 DataContext
UserControls
,它将覆盖继承的 DataContext
(甚至模糊有不同上下文的事实)。如果要在其声明中绑定 UserControl
的属性,请命名控件并使用 ElementName
或 RelativeSource
-bindings。
This is why you do not set the DataContext
of UserControls
, it will override the inherited DataContext
(and even obfuscate the fact that there is a different context). If you want to bind to properties of the UserControl
in its declaration then name the control and use ElementName
or RelativeSource
-bindings instead.
这篇关于在UserControl中设置DataContext正在影响父项中的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!