0
votes

I have a UserControl with one dependency property. This UserControl is used inside a view that already have a ViewModel as a DataContext. I'm currently binding one property from my top datacontext to the dependencyproperty. But now I would like to bind the same dependencyproperty to a property from the UserControl's Datacontext. In the end I want a binding between 2 properties of my DataContext's - the view's and the usercontrol's.

How could I achive this?

1
do you mean you want to bind 2 properties of your UserControl one to the property in DataContext of UserControl and other to the property in the DataContext of your Window.yo chauhan
To simplify the question, I have a UserControl with a DependencyProperty AND a Data context. How can I bind the DependencyProperty with some property from the datacontext?Louro

1 Answers

2
votes

Try one of the following way of binding

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

Above are the many types of binding . You can use one which suits your requirement .I hope this will help.