title | description | ms.date | helpviewer_keywords | ms.assetid | ||
---|---|---|---|---|---|---|
How to: Use the Master-Detail Pattern with Hierarchical Data |
Learn how to implement the master-detail scenario using the master-detail pattern with hierarchial data. |
03/30/2017 |
|
11429b9e-058d-4084-bfb6-2cf209c8ddf7 |
This example shows how to implement the master-detail scenario.
In this example, LeagueList
is a collection of Leagues
. Each League
has a Name
and a collection of Divisions
, and each Division
has a name and a collection of Teams
. Each Team
has a team name.
[!code-xamlMasterDetail#HowTo1]
[!code-xamlMasterDetail#HowTo2]
The following is a screenshot of the example. The Divisions
xref:System.Windows.Controls.ListBox automatically tracks selections in the Leagues
xref:System.Windows.Controls.ListBox and display the corresponding data. The Teams
xref:System.Windows.Controls.ListBox tracks selections in the other two xref:System.Windows.Controls.ListBox controls.
The two things to notice in this example are:
-
The three xref:System.Windows.Controls.ListBox controls bind to the same source. You set the xref:System.Windows.Data.Binding.Path%2A property of the binding to specify which level of data you want the xref:System.Windows.Controls.ListBox to display.
-
You must set the xref:System.Windows.Controls.Primitives.Selector.IsSynchronizedWithCurrentItem%2A property to
true
on the xref:System.Windows.Controls.ListBox controls of which the selection you are tracking. Setting this property ensures that the selected item is always set as the xref:System.Windows.Controls.ItemCollection.CurrentItem%2A. Alternatively, if the xref:System.Windows.Controls.ListBox gets it data from a xref:System.Windows.Data.CollectionViewSource, it synchronizes selection and currency automatically.
The technique is slightly different when you are using XML data. For an example, see Use the Master-Detail Pattern with Hierarchical XML Data.
- xref:System.Windows.HierarchicalDataTemplate
- Bind to a Collection and Display Information Based on Selection
- Data Binding Overview
- Data Templating Overview
- How-to Topics