1

I am newbie to WPF and curious to know how to nested bind the properties in WPF. I have made a sample app for better understanding. Please find below the following scenario's.

Working Code:

UserControl1 >> MainWindow

Created a UserControl and included it in the mainWindow.

UserControl1.xaml

<StackPanel>
     <TextBox x:Name="text1" Text="{Binding MyTextUC1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
     <TextBox />
</StackPanel>

UserControl1.xaml.cs

 public UserControl1()
 {
      InitializeComponent();
      this.DataContext = this;
 }

 public static readonly DependencyProperty MyTextUC1Property =
        DependencyProperty.Register("MyTextUC1", typeof(string),    typeof(UserControl1));

public string MyTextUC1
{
   get { return (string)GetValue(MyTextUC1Property); }
   set { SetValue(MyTextUC1Property, value); }
}

MainWindwo.xaml

<StackPanel>
    <local:UserControl1 x:Name="text1"/>
    <TextBlock Text="{Binding ElementName=text1, Path=MyTextUC1}" />
</StackPanel>

On running this, text changes in the TextBox of UserControl1 got reflected in TextBlock of MainWindow.

Not Working:

UserControl1 >> UserControl2 >> MainWindow

Created a UserControl(usercontrol1) and included it in another UserControl(usercontrol2) and then included the UserControl2 in mainWindow.

UserControl1.xaml & UserControl1.xaml.cs

//Same as Working Code

UserControl2.xaml

<Grid>
    <local:UserControl1 x:Name="text2" MyTextUC1="{Binding MyTextUC2,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

UserControl2.xaml.cs

 public UserControl2()
 {
     InitializeComponent();
     this.DataContext = this;
 }

 public static readonly DependencyProperty MyTextUC2Property =
     DependencyProperty.Register("MyTextUC2", typeof(string), typeof(UserControl2));       

 public string MyTextUC2
 {
     get { return (string)GetValue(MyTextUC2Property); }
     set { SetValue(MyTextUC2Property, value); }
 }

MainWindwo.xaml

<StackPanel>
    <local:UserControl2 x:Name="text1"/>   //UserControl2 contains UserControl1
    <TextBlock Text="{Binding ElementName=text1, Path=MyTextUC2}" />
</StackPanel>

On Running this, text changes in the TextBox of UserControl2 NOT REFLECTED in TextBlock of MainWindow.

I'm pretty sure that this is a basic thing in WPF and I may be implementing this in wrong way. Can someone suggest me where I am wrong? Thanks in advance.

1 Answer 1

2

If you check the Debug Output you will see the following error:

System.Windows.Data Error: 40 : BindingExpression path error: 'MyTextUC2' property not found on 'object' ''UserControl1' (Name='text2')'. BindingExpression:Path=MyTextUC2; DataItem='UserControl1' (Name='text2'); target element is 'UserControl1' (Name='text2'); target property is 'MyTextUC1' (type 'String')

The problem is with this binding:

<local:UserControl1 x:Name="text2" MyTextUC1="{Binding MyTextUC2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

It is looking for the MyTextUC2 property in UserControl1. It is because you set the DataContext in UserControl1's constructor to this (UserControl1). So every binding on UserControl1 will be evaluated against UserControl1's DataContext (which is itself).

If you change it to this:

<local:UserControl1 x:Name="text2" 
                    MyTextUC1="{Binding MyTextUC2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type local:UserControl2}}}" />

it should start working.

Sign up to request clarification or add additional context in comments.

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.