Wpf 将dependencyproperty从usercontrol绑定到其datacontext
我有一个带有一个依赖属性的UserControl。此UserControl在已将ViewModel用作DataContext的视图中使用。 我当前正在将一个属性从我的顶级datacontext绑定到dependencyproperty。但是现在我想将相同的dependencyproperty绑定到UserControl的Datacontext中的属性。 最后,我希望在DataContext的两个属性(视图和用户控件)之间进行绑定Wpf 将dependencyproperty从usercontrol绑定到其datacontext,wpf,binding,user-controls,dependency-properties,datacontext,Wpf,Binding,User Controls,Dependency Properties,Datacontext,我有一个带有一个依赖属性的UserControl。此UserControl在已将ViewModel用作DataContext的视图中使用。 我当前正在将一个属性从我的顶级datacontext绑定到dependencyproperty。但是现在我想将相同的dependencyproperty绑定到UserControl的Datacontext中的属性。 最后,我希望在DataContext的两个属性(视图和用户控件)之间进行绑定 我怎样才能做到这一点呢?试试下面的一种绑定方式 /
我怎样才能做到这一点呢?试试下面的一种绑定方式
// UserControl DataContext={Binding SomeDataContext } Suppose here UserControl starts
<!--Bind Height with Width of SameControl-->
<TextBox Name="textBox1" Height="{Binding Width, RelativeSource={RelativeSource Mode=Self}}"/>
<!--Bind Height to the VMProperty in the DataContext of Window-->
<TextBox Name="textBox2" Height="{Binding DataContext.VMProperty, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
<!--Bind Height with the Width of first textbox-->
<TextBox Name="textBox3" Height="{Binding Width, ElementName=textBox1}"/>
<!--bind Height with the UserControlDataContextProperty in UserControl DataContext-->
<TextBox Name="textBox4" Height="{Binding UserControlDataContextProperty}"/>
//Here UserControl ends
//UserControl-DataContext={Binding SomeDataContext}假设UserControl在这里启动
//用户控制到此结束
上面是许多类型的绑定。您可以使用一个适合您的要求的属性。我希望这会有所帮助。您的意思是要将UserControl的两个属性绑定到UserControl的DataContext中的属性,而将另一个绑定到窗口DataContext中的属性。为了简化问题,我有一个UserControl,它具有DependencyProperty和Data context。如何将DependencyProperty与datacontext中的某些属性绑定?