WPF: How to bind to a nested property?

You can also set DataContext for TextBox in XAML (I don’t know if it’s optimal solution, but at least you don’t have to do anything manually in codeBehind except of implementing INotifyPropertyChanged). When your TextBox has already DataContext (inherited DataContext) you write code like this:

<TextBox 
   DataContext="{Binding Path=ParentProperty}"
   Text="{Binding Path=ChildProperty, Mode=TwoWay}" 
   Width="30"/>

Be aware that until your DataContext for TextBox isn’t ready binding for Text property will not be ‘established’ – you can add FallbackValue="error" as Binding parameter – it will be something like indicator which will show you if binding is OK or not.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)