views:

49

answers:

2

Hi! I have a WPF TreeView with 2 levels of data and 2 HierarchicalDataTemplate to format each level. From the HierarchicalDataTemplate at the second level, I need to bind a property in the class of the first level. I have tried in this way, but it dosn't work:

Text="{Binding Path=Ori, RelativeSource={RelativeSource TemplatedParent}}" with "Ori" as the name of the propery

Even in this way it dosn't works: Text="{Binding Path=tOri, RelativeSource={RelativeSource TemplatedParent}}" with "tOri" as the name of the TextBlock in the fisrt HierarchicalDataTemplate that bind the "Ori" propery

Can you help me? Thanks, Pileggi

+1  A: 

TemplatedParent only refers to the parent Control inside a ControlTemplate and so doesn't work with DataTemplates. You can use FindAncestor instead to locate the parent TreeViewItem and then access its DataContext.

Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}, AncestorLevel=2}, Path=DataContext.Ori}"
John Bowen
Thank you! I tried your solution but it didn't work because I didn't know I have to write "AncestorLevel=2"
pileggi
A: 

You have misunderstood the TemplatedParent binding in WPF. TemplatedParent refers to the inherited control that you are extending. Example: if I wrote a ControlTemplate that targeted a Button.

<ControlTemplate TargetType="{x:Type Button}" x:Key="MyButtonTemplate">
   <Border BorderBrush="{TemplateBinding Property=Background}" BorderThickness="3" >
      <ContentPresenter Margin="10"/>
   </Border>
</ControlTemplate>

This is binding the BorderBrush to the the base Button.Background property.

To achieve what you want, you need to walk the visual tree using the RelativeSource FindAncestor to find the parent and then perform the binding. To help try using either Mole WPF or Snoop.

Note: the copy of Snoop available above has some serious issues, i.e., cannot go more than 256-levels deep. I have a patched and feature extended version that is awesome. A interchange between using Mole and Snoop2 to debug/visualise during development.

Dennis Roche
The newest version of Snoop is at http://snoopwpf.codeplex.com/ and has all of the combined fixes from the other versions out there in addition to 32 and 64 bit support and .NET 4.Be careful about confusing TemplateBinding with RelativeSource TemplatedParent. They are separate mechanisms that do similar things but TemplateBinding is much more restrictive in that it can only point to a property of matching type on the parent and doesn't have any of the other options available on Binding (Converters, complex Paths, etc).
John Bowen
@John: Thanks for the update on Snoop; I will grab the latest and integrate into my branch - perhaps there are fixes/features that I can contribute.
Dennis Roche
@John: Yes, I should have explained the difference between the two. `TemplateBinding` is far more limited, as you explained, and it is resolved at compile time whereas `RelativeSource TemplatedParent` is resolved at runtime (using the Binding engine).
Dennis Roche