GridView In WPF

WPF GridView control

If you are looking for a GridView control in WPF, you will be disappointed. WPF does not have a GridView control. But the good news is you can achieve GridView like functionality with a ListView control available in WPF.

If you have not used a ListView control in WPF, I recommend you read my ListView in WPF article before this article.

The View property is used to provide a GridView layout and design in a ListView control. The View property of ListView is a type of ViewBase class that supports two view types - GridView and a custom view. A GridView is used for arranging data in columns and adding layout and design support to a ListView.

The following code snippet sets the View property of a ListView to GridView mode.

  1. <ListView>  
  2.     <ListView.View>  
  3.         <GridView />  
  4.     </ListView.View>  
  5. </ListView>  

A GridView is used as a supplement control to a ListView to provide style and layout. The GridView does not have its own control related properties such as background and foreground colors, font properties, size, and location. The container ListView is used to provide all control related properties.

Creating a GridView

The GridView element in XAML represents a GridView at design-time. The Columns property of GridView returns a collection of GridViewColumn objects. GridViewColumn element in XAML represents a GridView column. AllowsColumnReorder property represents weather columns in a GridView and can be reordered by a user by dragging and dropping from one position to another. ColumnHeaderToolTip property represents the content of a tooltip that appears when the mouse pointer pauses over one of the column headers.

The code listed in Listing 1 creates a GridView control and adds four columns to it. The Header property of GridViewColumn represents the header of a column. The Width property represents the width of a column and the DisplayMemberBinding property is used to bind a GridViewColumn to a property of data binding object.

  1. <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors">  
  2.     <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />  
  3.     <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Path=Age}" />  
  4.     <GridViewColumn Header="Book" Width="250" DisplayMemberBinding="{Binding Path=Book}" />  
  5.     <GridViewColumn Header="MVP" Width="50" DisplayMemberBinding="{Binding Path=Mvp}" />  
  6. </GridView>  

Listing 1

The output of listing looks like Figure 1.

Creating a GridView
Figure 1

The GridCiew class in WPF represents a GridView control. The GridViewColumn class represents a GridView column. The code listed in Listing 2 creates a GridView control and adds four columns to it dynamically.

  1. privatevoid CreateDynamicGridView() {  
  2.     // Create a GridView  
  3.     GridView grdView = newGridView();  
  4.     grdView.AllowsColumnReorder = true;  
  5.     grdView.ColumnHeaderToolTip = "Authors";  
  6.     GridViewColumn nameColumn = newGridViewColumn();  
  7.     nameColumn.DisplayMemberBinding = newBinding("Name");  
  8.     nameColumn.Header = "Author Name";  
  9.     nameColumn.Width = 120;  
  10.     grdView.Columns.Add(nameColumn);  
  11.     GridViewColumn ageColumn = newGridViewColumn();  
  12.     ageColumn.DisplayMemberBinding = newBinding("Age");  
  13.     ageColumn.Header = "Age";  
  14.     ageColumn.Width = 30;  
  15.     grdView.Columns.Add(ageColumn);  
  16.     GridViewColumn bookColumn = newGridViewColumn();  
  17.     bookColumn.DisplayMemberBinding = newBinding("Book");  
  18.     bookColumn.Header = "Book";  
  19.     bookColumn.Width = 250;  
  20.     grdView.Columns.Add(bookColumn);  
  21.     GridViewColumn mvpColumn = newGridViewColumn();  
  22.     mvpColumn.DisplayMemberBinding = newBinding("Mvp");  
  23.     mvpColumn.Header = "Mvp";  
  24.     mvpColumn.Width = 50;  
  25.     grdView.Columns.Add(mvpColumn);  
  26.     ListView1.View = grdView;  
  27. }  

Listing 2

Adding a ContextMenu to a GridView Header

The ColumnHeaderContextMenu property of GridView is used to get or set a ContextMenu when you click on the header of a GridView control. The code snippet listed in Listing 3 adds a ContextMenu to a GridView header.

  1. <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors">  
  2.     <!-- Add a ContextMenu to GridView Header -->  
  3.     <GridView.ColumnHeaderContextMenu>  
  4.         <ContextMenu>  
  5.             <MenuItem Header="Ascending" Click="MenuItem_Click" />  
  6.             <MenuItem Header="Descending" />  
  7.         </ContextMenu>  
  8.     </GridView.ColumnHeaderContextMenu>  
  9.     <!-- Add GridVeiw Columns -->  
  10.     <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />  
  11.     <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Path=Age}" />  
  12.     <GridViewColumn Header="Book" Width="250" DisplayMemberBinding="{Binding Path=Book}" />  
  13.     <GridViewColumn Header="MVP" Width="50" DisplayMemberBinding="{Binding Path=Mvp}" />  
  14. </GridView>  

Listing 3

The output of listing looks like Figure2.

ContextMenu to a GridView Header
Figure 2

Adding a CheckBox to a GridView Header

The ColumnHeaderTemplate property represents the template for the column header. Using this property, you may format GridView column headers the way you want. You may add CheckBoxes or Images to the column headers.

The code snippet in Listing 4 creates a DataTemplate resource with a CheckBox and orange foreground color of text that we can set as ColumnHeaderTemplate of a GridView.

  1. <Window.Resources>  
  2.     <DataTemplate x:Key="OrangeHeaderTemplate">  
  3.         <DockPanel>  
  4.             <CheckBox />  
  5.             <TextBlock FontSize="10" Foreground="Orange" FontWeight="Bold">  
  6.                 <TextBlock.Text>  
  7.                     <Binding />  
  8.                 </TextBlock.Text>  
  9.             </TextBlock>  
  10.         </DockPanel>  
  11.     </DataTemplate>  
  12. </Window.Resources>  

Listing 4

The code snippet in Listing 5 sets the ColumnHeaderTemplate of a GridView.

  1. <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors" ColumnHeaderTemplate="{StaticResource OrangeHeaderTemplate}" >  

Listing 5

The output of listing looks like Figure 3.

ColumnHeaderTemplate of a GridView
Figure 3

Summary

This article was an introduction of using a GridView in WPF. I will be discussing more GridView related functionality in my forthcoming articles.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.