0

我有一个具有一个依赖属性的 UserControl。此 UserControl 用于已将 ViewModel 作为 DataContext 的视图中。我目前正在将我的顶级数据上下文中的一个属性绑定到依赖属性。但现在我想将相同的依赖属性绑定到来自 UserControl 的 Datacontext 的属性。最后,我想要我的 DataContext 的 2 个属性之间的绑定 - 视图和用户控件的。

我怎么能做到这一点?

4

1 回答 1

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

以上是多种绑定方式。您可以使用适合您要求的。我希望这会有所帮助。

于 2012-07-18T14:49:02.343 回答