Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 2.91 KB

how-to-use-the-master-detail-pattern-with-hierarchical-data.md

File metadata and controls

39 lines (27 loc) · 2.91 KB
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
data binding [WPF], Master-Detail data paradigm
Master-Detail data paradigm
11429b9e-058d-4084-bfb6-2cf209c8ddf7

How to: Use the Master-Detail Pattern with Hierarchical Data

This example shows how to implement the master-detail scenario.

Example

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.

Screenshot that shows a Master-detail scenario example.

The two things to notice in this example are:

  1. 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.

  2. 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.

See also