Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

How to: Use the Master-Detail Pattern with WPF Hierarchical DataTemplate

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
7 Apr 2014CPOL2 min read 35.1K   1.6K   44   6
This article shows a simple way to implement Master-Detail pattern with hierarchical DataTemplate
Image 1

Introduction

This article shows a practical & simple way to implement Master-Detail pattern with hierarchical DataTemplate.

Background

Master-Detail is a frequently used pattern in computer science and information systems. Almost every data oriented project is using this pattern. The principle is very simple, showing list of details by selected master item:

Image 2

We can implement Master-Detail in multiple ways to achieve this pattern, in my opinion the easiest way to do so in WPF is using DataTemplate.

Representing the Hierarchical Data

Every Master-Detail pattern requires a data structure. As an example of a hierarchical data-structure, I’ve chosen to use the following hypothetical Hi-Tech company:<o:p>

Image 3

The company is comprised of three departments:<o:p>

  • Management: CEO, CTO, VP.QA
  • Development: CTO and subordinates on the development team.
  • QA: VP of QA and subordinates on the QA team.

For this example, I've chosen to take the aspect of the departments, i.e. every row in the view is a different department:

Image 4

Let's look at the classes:

C#
public class Employee
{
    public Employee()
    {
        Subordinates = new List<Employee>();
    }

    public List<Employee> Subordinates { get; set; }

    public string Name
    {
        get;
        set;
    }

    public override string ToString()
    {
        return this.Name;
    }
} 

Each employee includes “subordinates” which is a collection of employees.<o:p>

C#
public class Department : INotifyPropertyChanged
{
    public Department()
    {
        Employees = new List<Employee>();
    }

    public List<Employee> Employees { get; set; }

    protected int m_EmployeesSelectedIndex = 0;

    public int EmployeesSelectedIndex
    {
        get
        {
            return m_EmployeesSelectedIndex;
        }
        set
        {
            m_EmployeesSelectedIndex = value;
            NotifyPropertyChanged("Subordinates");
        }
    }

    public List<Employee> Subordinates
    {
        get
        {
            List<Employee> res = null;
            if (Employees.Count > 0)
            {
                res = Employees[EmployeesSelectedIndex].Subordinates;
            }

            return res;
        }
    }

    public string Name
    {
        get;
        set;
    }

    public override string ToString()
    {
        return this.Name;
    }

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
} 

Every department includes a collection of the employees working there.<o:p>

The Department class also contains the following members:

  1. EmployeesSelectedIndex
  2. Subordinates list
  3. NotifyPropertyChanged mechanism

All of these are used for implementing the Master-Detail Logic, and will be explained in the section "Master-Detail Logic" below.<o:p>

Presenting the Data (UI):

For the sake of simplicity, we are presenting the data inside an ItemsControl:<o:p>

HTML
<!-- Data rows -->
<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" MinWidth="320">                    
                <TextBlock Width="130" Text="{Binding Name}"/>          
                <ComboBox Width="130" ItemsSource="{Binding Employees}" SelectedIndex="{Binding EmployeesSelectedIndex}" />         
                <ComboBox Width="130" ItemsSource="{Binding Subordinates}" />            
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl> 

So what we are dealing with here is simply a DataTemplate with:

  • TextBlock - For the department name.
  • ComboBox - For the departments’ employees’ names.
  • ComboBox - For the selected employee’s subordinates.

Please Notice: In the code sample attached for download, there are also headers, borders & other simple UI decorations for each data item, but the logic is the same.

It should look like this:

Image 5

Master-Detail Logic:

We need to connect the selection of a particular employee to that employee’s subordinates list.

That is why the Department class contains:<o:p>

  1. EmployeesSelectedIndex
  2. Subordinates list
  3. NotifyPropertyChanged mechanism

Let’s see how it works:<o:p>

Image 6

About the examples for download:

The examples for download are a bit more complicated:

Master-Detail Simple file also contains:

  • Headers, borders & other simple UI decorations for each data item.
  • Subordinates Selected-Index for default selection of the first index in the Subordinates Combo-Box.
  • Subordinates Is-Enabled for disabling the Subordinates Combo-Box when there are no Items.

Image 7

Master-Detail Advanced file also contains:

  • all of the above (from the Simple example)
  • Employee-Image & Subordinate-Image

Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer GalilCS
Israel Israel
My name is Shai Vashdi and I’m the CTO & Co-Founder of GalilCS.
GalilCS provides industrialized, low-cost IT services (ILCS) to corporations around the globe. We specialize in code modernization projects (VB6 to C# WinForms/HTML5, for example) and code refactoring projects.
Most of my work revolves around the management of these projects and the creation of new tools for refactoring code. As a bonus, I also get to lecture about Microsoft programming languages.
For more information, visit GalilCS.

Comments and Discussions

 
QuestionHierarchical Data ? Pin
Master.Man198016-Apr-14 7:46
Master.Man198016-Apr-14 7:46 
AnswerRe: Hierarchical Data ? Pin
Shai Vashdi16-Apr-14 8:40
Shai Vashdi16-Apr-14 8:40 
GeneralMy vote of 5 Pin
elior keren8-Apr-14 23:46
elior keren8-Apr-14 23:46 
GeneralMy vote of 3 PinPopular
yoav bernoli8-Apr-14 3:17
yoav bernoli8-Apr-14 3:17 
GeneralRe: My vote of 3 Pin
Shai Vashdi8-Apr-14 3:58
Shai Vashdi8-Apr-14 3:58 
GeneralRe: My vote of 3 Pin
#realJSOP4-Sep-16 5:15
mve#realJSOP4-Sep-16 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.