Bind to Parent Object Property with RelativeSource

Stef picture Stef · Oct 22, 2012 · Viewed 13.9k times · Source

I've built a WPF based Treeview with

Item
-Subitem

If Subitem is selected, I would like to display also Properties of Item.

<StackPanel Grid.Column="2" DataContext="{Binding ElementName=myTreeView, Path=SelectedItem}">
  <TextBox Text="{Binding Path=Name, Mode=TwoWay}" />
  <TextBox Text="{Binding RelativeSource={???} Path=Name, Mode=TwoWay}" />
</StackPanel>

I guess I need to use a RelativeSource statement, but not quite sure how to do so.

Answer

opewix picture opewix · Oct 22, 2012
 {Binding RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}, Path=Name, Mode=TwoWay}