0

1 つの依存関係プロパティを持つ UserControl があります。この UserControl は、既に ViewModel を DataContext として持つビュー内で使用されます。現在、最上位のデータ コンテキストから依存関係プロパティに 1 つのプロパティをバインドしています。しかし今、同じ依存関係プロパティを 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 に答える