Click here to Skip to main content
11,647,810 members (61,732 online)
Click here to Skip to main content

WPF Toolkit DataGrid Supporting Multiple Column Sets

, 12 Jul 2010 CPOL 30.1K 1.1K 21
Rate this:
Please Sign up or sign in to vote.
WPF Toolkit DataGrid supporting multiple column sets

Introduction

The WPF Toolkit's DataGrid is a powerful grid control allowing to format data in various ways.

In my application, I wanted the user to be able to control which data he gets displayed in the DataGrid. To do so, he can open an options dialog and choose between some radio buttons. Each radio button defines a set of data columns displayed in the DataGrid.

While it is possible to define multiple column sets by code (DataGrid.Columns property), the DataGridonly allows to define one column set via XAML.

The MultiColSetDataGridcontrol described in this article helps to overcome this limitation.

Background

The class MultiColSetDataGridhas a property ColumnsDefinitionListcontaining a list of column sets and a property RowDetailsTemplateListcontaining a list of DataTemplesdisplayed in the row details area.

A further property GridColumnsDefinitionsIndexcontrols which column set and row details DataTemplateis currently set. This property can be databinded by view models.

Using the Code

The MainWindow.xaml file contains the MultiColumnSetDataGridcontrol:

<ctrl:MultiColSetDataGrid x:Name="myGrid" 
    ItemsSource="{Binding PersonList}" 
    ....

The MainWindowVM's (view model) property PersonListis used as ItemsSource. The list is a list of persons. Each entry is of type PersonVMoffering FirstNameand LastNameas Stringproperties.

Two sets of columns are defined in the MainWindow.xaml:

<ctrl:MultiColSetDataGrid.ColumnsDefinitionList>
  <ctrl:ColumnsDefinition>
    <ctrl:ColumnsDefinition.GridColumns>
      <tk:DataGridTextColumn Width="Auto" 
        Header="First Name" 
        Binding="{Binding FirstName}" />
    </ctrl:ColumnsDefinition.GridColumns>
  </ctrl:ColumnsDefinition>
  <ctrl:ColumnsDefinition>
    <ctrl:ColumnsDefinition.GridColumns>
      <tk:DataGridTextColumn Width="Auto" 
        Header="First Name" 
        Binding="{Binding FirstName}" />
      <tk:DataGridTextColumn Width="Auto" 
        Header="Last Name" 
        Binding="{Binding LastName}" />
    </ctrl:ColumnsDefinition.GridColumns>
  </ctrl:ColumnsDefinition>
</ctrl:MultiColSetDataGrid.ColumnsDefinitionList>

Two DataTemplates are defined for the row details section:

<ctrl:MultiColSetDataGrid.RowDetailsTemplateList>
  <DataTemplate>
    <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
      <TextBlock Text="Name: " FontWeight="Bold"/>
      <TextBlock Text="{Binding FirstName}"/>
    </StackPanel>
  </DataTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
      <TextBlock Text="Name: " FontWeight="Bold"/>
      <TextBlock Text="{Binding LastName}"/>
      <TextBlock Text=", "/>
      <TextBlock Text="{Binding FirstName}"/>
    </StackPanel>
  </DataTemplate>
</ctrl:MultiColSetDataGrid.RowDetailsTemplateList>

A button is used to switch between the column sets:

<Button Width="120" Command="{Binding CmdSwitch}" Content="Switch Column Sets" />

The MainWindowVMhas an integer property CurrentColumnSetIndex, which determines the column set currently used. This property is binded to the MultiColumnSetDataGrid's dependency property GridColumnsDefinitionsIndex.

<ctrl:MultiColSetDataGrid x:Name="myGrid" 
    ...
    GridColumnsDefinitionsIndex="{Binding CurrentColumnSetIndex, Mode=TwoWay}">

The ViewModel's ICommandbound to the button switches the CurrentColumnSetIndexvalue from 0 to 1 and vice versa.

public ICommand CmdSwitch
{
  get
  {
    return new RelayCommand(p => DoSwitch());
  }
}

private void DoSwitch()
{
  if (this.CurrentColumnSetIndex == 0)
    this.CurrentColumnSetIndex = 1;
  else
    this.CurrentColumnSetIndex = 0;
}

The setter of MultiColSetDataGrid's property GridColumnsDefinitionsIndexis called via databinding and calls method LoadColumns...

public Int32 GridColumnsDefinitionsIndex
{
  get
  {
    return (Int32)this.GetValue(GridColumnsDefinitionsIndexProperty);
  }
  set
  {
    this.SetValue(GridColumnsDefinitionsIndexProperty, value);
    LoadColumns();
  }
}

And in method LoadColumnsthe defined column set behind the index is loaded:

private void LoadColumns()
{
  int tmpIndex = this.GridColumnsDefinitionsIndex;
  if (this.ColumnsDefinitionList == null || tmpIndex < 0 || 
		tmpIndex >= this.ColumnsDefinitionList.Count)
    return;

  // Load column set
  List<DataGridColumn> tmpColumns = 
		this.ColumnsDefinitionList[tmpIndex].GridColumns;
  this.Columns.Clear();
  foreach (var tmpColumn in tmpColumns)
    this.Columns.Add(tmpColumn);

  // Set RowDetailsTemplate
  if (this.RowDetailsTemplateList == null || tmpIndex < 0 || 
		tmpIndex >= this.RowDetailsTemplateList.Count)
    return;
  this.RowDetailsTemplate = this.RowDetailsTemplateList[tmpIndex];
}

The following screenshot shows the application after startup containing the column set with index 0 displaying the "First Name" column only:

grid_with_1_column.jpg

The following screenshot shows the application after having clicked on the "Switch column sets" button. The grid is displaying the"First Name" and "Last Name" columns:

grid_with_2_cols.jpg

History

  • Initial version

License

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

Share

About the Author

Frank Augustin
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Tamil Selvan K3-Jul-12 3:54
memberTamil Selvan K3-Jul-12 3:54 
Questionvery nice Pin
CIDev7-Nov-11 9:19
memberCIDev7-Nov-11 9:19 
Generalconvert to VS2008 Pin
toilaleanh28-Jan-11 3:19
membertoilaleanh28-Jan-11 3:19 
GeneralGreat solution Pin
granty22-Sep-10 17:48
membergranty22-Sep-10 17:48 
GeneralMy vote of 5 Pin
tripathy.rajendra@gmail.com21-Sep-10 21:31
membertripathy.rajendra@gmail.com21-Sep-10 21:31 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 12 Jul 2010
Article Copyright 2010 by Frank Augustin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid