0

I have created the following User Control:

<UserControl x:Class="TextBinder" ...>
    <TextBox Text="{Binding ????}" />
</UserControl>

Now I am using my user control twice in my MainWindow. The MainWindow is then bound to my ViewModel (I set the DataContext). Now the problem is: how can I bind my user controls to the user_controlViewModel?

In my ViewModel, I have created two objects let's call them UC_1 and UC_2, they contain different texts and I would like to bind them to their respective user control in my MainWindow.

What should I put at ????

Note: please do not simplify mu TextBox to double textboxes in one usercontrol. This is not what I would like since in my real life example I have more stuff than textbox only and the usercontrol should be used multiple times in one view.

Thanks!

2
  • 1
    Define dependency property in you user control. Commented Dec 18, 2014 at 13:43
  • So your MainWindow has DataContext set to ViewModel if that's the case then your <UserControl.DataContext="{Binding UC_1}"/> then your <TextBox Text="{Binding propertyOnYourUC_1}"/> try that, without DependencyProperty. HTH Commented Dec 18, 2014 at 14:23

2 Answers 2

2

i gave you a general answer:

within a "real(a usercontrol you wanna use with different viewmodels with different property names)" usercontrol you bind just to your own DependencyProperties and you do that with ElementName or RelativeSource binding and you should never set the DataContext within a UserControl.

 <UserControl x:Name="myRealUC" x:class="MyUserControl">
   <TextBox Text="{Binding ElementName=myRealUC, Path=MyOwnDPIDeclaredInMyUc, Path=TwoWay}"/>
 <UserControl>

if you do that you can easily use this Usercontrol in any view like:

<myControls:MyUserControl MyOwnDPIDeclaredInMyUc="{Binding MyPropertyInMyViewmodel}"/>

and for completeness: the Dependency Property

    public readonly static DependencyProperty MyOwnDPIDeclaredInMyUcProperty = DependencyProperty.Register(
    "MyOwnDPIDeclaredInMyUc", typeof(string), typeof(MyUserControl), new PropertyMetadata(""));

    public bool MyOwnDPIDeclaredInMyUc
    {
        get { return (string)GetValue(MyOwnDPIDeclaredInMyUcProperty); }
        set { SetValue(MyOwnDPIDeclaredInMyUcProperty, value); }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Thats right, you need yo declare a dependency property in your UserControl:

public partial class TextBinder:UserControl
{

  public static readonly DependencyProperty textproperty = 
  DependencyProperty.Register("Text", typeof(string), typeof(TextBinder));

  public string Text
  {
    get
    {
        return this.GetValue(textproperty) as string;
    }
    set
    {
        this.SetValue(textproperty,value);
    }
  }
}

And then, you can use your usercontrol in your window at this way:

<YourNamespace:TextBinder Text={Binding ViewModelProperty}/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.