在 UserControl 中设置 DataContext 会影响父级中的绑定 [英] Setting DataContext within UserControl is affecting bindings in parent

查看:22
本文介绍了在 UserControl 中设置 DataContext 会影响父级中的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的 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 路径错误:在 'object' ''ChildControl' (Name='ChildName')' 上找不到 'PropertyInParentContext' 属性.BindingExpression:Path=PropertyInParentContext;DataItem='ChildControl' (Name='ChildName');目标元素是 'ChildControl' (Name='ChildName');目标属性是 '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')

为什么要在子控件中而不是在父控件的 DataContext 中查找PropertyInParentContext"?

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>

这就是为什么你没有设置UserControlsDataContext,它会覆盖继承的DataContext(甚至混淆了这样一个事实)是不同的上下文).如果您想在其声明中绑定到 UserControl 的属性,请命名控件并改用 ElementNameRelativeSource-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屋!

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