2012-07-18 118 views
0

我有一个UserControl具有一个依赖项属性。这个UserControl用在已经有ViewModel作为DataContext的视图中。 我目前从我的顶级datacontext绑定一个属性到dependencyproperty。但是现在我想将相同的依赖属性绑定到UserControl的Datacontext的属性。 最后,我想要在我的DataContext的两个属性 - 视图和usercontrol之间进行绑定。从usercontrol绑定dependencyproperty到它的datacontext

我怎么能实现这个?

+0

你的意思是你想要将UserControl的2个属性绑定到UserControl的DataContext的属性中,并将其他属性绑定到Window的DataContext中的属性。 – ethicallogics 2012-07-18 14:11:25

+0

为了简化问题,我有一个带有DependencyProperty和Data上下文的UserControl。我怎样才能绑定DependencyProperty与datacontext的一些属性? – Louro 2012-07-18 14:36:46

回答

2

尝试的结合

 // 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 

以上是许多类型的结合下面的方式之一。你可以使用一个适合你的要求。我希望这会有所帮助。

相关问题