Skip to Main Content
Programming WPF, 2nd Edition
book

Programming WPF, 2nd Edition

by Chris Sells, Ian Griffiths
August 2007
Intermediate to advanced content levelIntermediate to advanced
864 pages
25h 52m
English
O'Reilly Media, Inc.
Content preview from Programming WPF, 2nd Edition

Hierarchical Binding

Master-detail binding is one step away from true hierarchical binding in that it generally involves a known set of levels. For example, when we wanted to go from two levels to three levels, we added another column to the table and manually set up the relationship at the new level. On the other hand, hierarchical binding (sometimes called tree binding) generally involves some number of levels that aren't known until runtime and a control that can expand itself as appropriate, like a menu or a tree. WPF has built-in support for hierarchical binding using a special kind of data template that knows both how to display the current level of data and where to go for the next level. It's a bit involved, though, so let's go back to first principles with our family data (see Example 7-51).

Example 7-51. The beginnings of hierarchical data binding

<Window ...>
  <Window.Resources>
    <local:Families x:Key="Families">
      ...
    </local:Families>
  </Window.Resources>

  <TreeView DataContext="{StaticResource Families}">
    <TreeViewItem ItemsSource="{Binding}" Header="Families" />
  </TreeView>
</Window>

In Example 7-51, we're binding a TreeView control's root item to the top level of the families data, labeling the root "Families," as shown in Figure 7-5.

The beginnings of hierarchical data binding

Figure 7-25. The beginnings of hierarchical data binding

Because the Families collection contains two Family objects, but we haven't provided ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming C#, 4th Edition

Programming C#, 4th Edition

Jesse Liberty
Programming C# 10

Programming C# 10

Ian Griffiths

Publisher Resources

ISBN: 9780596510374Supplemental ContentErrata Page