2012-07-18 113 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 

以上是許多類型的結合下面的方式之一。你可以使用一個適合你的要求。我希望這會有所幫助。

相關問題