Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何避免绑定到2 datacontext_C#_Wpf_User Controls_Dependency Properties - Fatal编程技术网

C# 如何避免绑定到2 datacontext

C# 如何避免绑定到2 datacontext,c#,wpf,user-controls,dependency-properties,C#,Wpf,User Controls,Dependency Properties,我的应用程序如下所示 黑色是我的主窗口,红色是选项卡控件,黄色是用户控件 UserControl定义了许多依赖项属性,它们绑定到DataContext(使用this.DataContext=this在主窗口的代码隐藏中设置) 要将我的UserControl绑定到与主窗口相同的DataContext,在我的UserControl xaml中,我有以下内容 DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor,

我的应用程序如下所示

黑色是我的主窗口,红色是选项卡控件,黄色是用户控件

UserControl定义了许多依赖项属性,它们绑定到DataContext(使用
this.DataContext=this
在主窗口的代码隐藏中设置)

要将我的UserControl绑定到与主窗口相同的DataContext,在我的UserControl xaml中,我有以下内容

DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=Window}}"
这非常有效,当我与我的UserControl交互时,由于双向绑定,它会更新我的主窗口的属性,而主窗口又会更新我的TabControl

问题是,我的UserControl现在有一些额外的功能,因此需要绑定到UserControl的代码(例如GUI的值)

这就是我被困的地方。我无法从UserControl绑定到代码隐藏,因为我已经创建了DataContext

我知道我可以使用WinForms方法,用x:name=“MyControl”来命名每个控件,比如

我想是的


我的问题是,这是唯一的出路,还是我仍然可以使用绑定

您可以使用另一个
相对资源绑定
,就像您对
主窗口
属性所做的那样。。。要访问
UserControl
中定义的属性,请在
UserControl
中尝试以下XAML:

<TextBlock Text="{Binding UserControlProperty, RelativeSource={RelativeSource 
    AncestorType={x:Type YourXmlNamespacePrefix:YourUserControl}}}" />

显然,您需要将
YourXmlNamespacePrefix:YourUserControl
更新为有效的XML名称空间和控件类型,以使其正常工作



我不是说您应该在任何地方设置
DataContext
,或者更改任何属性。这是一个
相对资源绑定
。。。无需设置任何
DataContext
即可使其正常工作。我以为你已经知道了,因为你已经在用了。试试这个例子。

首先,您不需要在UserControl上手动设置
DataContext
DataContext
是一个可继承属性,因此除非您明确设置它,否则它将从其父级继承DataContext

从用户控件中删除
DataContext=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,antestorlevel=1,antestortype=Window}”


现在,如果您想为UserControl中的某些控件绑定到代码隐藏,您可以使用
RelativeSource
或在控件上设置
DataContext

DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                                     AncestorType=UserControl}}"
如果控件可以在一个面板下组合在一起,则在父面板上设置DataContext,表示网格和子控件将从中继承:

<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType=UserControl}}">
   ..Child Controls here will inherit DataContext
</Grid>

…此处的子控件将继承DataContext

回答你的问题:


MyControl.DataContext=this;对不起,我迷路了。因此,这意味着,我的UserControl将绑定到它自己的DataContext(在本例中是隐藏的代码),并且我为UserControl中需要绑定到MainWindow的DataContext的每个控件使用RelativeSource?我的主窗口只有这个。DataContext=这个;我知道这是遗传的-你以前告诉过我,但我仍然坚持要把它放进去!!叹息。。。总有一天我会学会的!!你又帮我修好了!谢谢。哦,是吗?我猜它会在圆圈里回到你身边……)不客气,戴夫。。!!祝你今天愉快……)
DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                                     AncestorType=UserControl}}"
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                            AncestorType=UserControl}}">
   ..Child Controls here will inherit DataContext
</Grid>