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.
Figure 7-25. The beginnings of hierarchical data binding
Because the Families collection contains two Family
objects, but we haven't provided ...
Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.