Using XAML GridView in WPF

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, a 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 supplemental 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 the 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. A GridViewColumn element in XAML represents a GridView column. The AllowsColumnReorder property represents whether columns in a GridView can be reordered by a user by dragging and dropping from one position to another. The 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.           >  
  3. <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />  
  4.     <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Path=Age}" />  
  5.     <GridViewColumn Header="Book" Width="250" DisplayMemberBinding="{Binding Path=Book}" />  
  6.     <GridViewColumn Header="MVP" Width="50" DisplayMemberBinding="{Binding Path=Mvp}" />  
  7.       
  8. </GridView>  
                                     Listing 1

The output of listing looks as in Figure 1.

listing looks like
                                                                 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. private void CreateDynamicGridView()  
  2. {  
  3.     // Create a GridView    
  4.     GridView grdView = new GridView();  
  5.     grdView.AllowsColumnReorder = true;  
  6.     grdView.ColumnHeaderToolTip = "Authors";  
  7.   
  8.     GridViewColumn nameColumn = new GridViewColumn();  
  9.     nameColumn.DisplayMemberBinding = new Binding("Name");  
  10.     nameColumn.Header = "Author Name";  
  11.     nameColumn.Width = 120;  
  12.     grdView.Columns.Add(nameColumn);  
  13.   
  14.     GridViewColumn ageColumn = new GridViewColumn();  
  15.     ageColumn.DisplayMemberBinding = new Binding("Age");  
  16.     ageColumn.Header = "Age";  
  17.     ageColumn.Width = 30;  
  18.     grdView.Columns.Add(ageColumn);  
  19.   
  20.     GridViewColumn bookColumn = new GridViewColumn();  
  21.     bookColumn.DisplayMemberBinding = new Binding("Book");  
  22.     bookColumn.Header = "Book";  
  23.     bookColumn.Width = 250;  
  24.     grdView.Columns.Add(bookColumn);  
  25.   
  26.     GridViewColumn mvpColumn = new GridViewColumn();  
  27.     mvpColumn.DisplayMemberBinding = new Binding("Mvp");  
  28.     mvpColumn.Header = "Mvp";  
  29.     mvpColumn.Width = 50;  
  30.     grdView.Columns.Add(mvpColumn);  
  31.   
  32.     ListView1.View = grdView;  
  33. }  
                                                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.       
  3.     <!-- Add a ContextMenu to GridView Header -->  
  4.     <GridView.ColumnHeaderContextMenu>  
  5.         <ContextMenu >  
  6.             <MenuItem Header="Ascending" Click="MenuItem_Click" />  
  7.             <MenuItem Header="Descending" />  
  8.         </ContextMenu>  
  9.     </GridView.ColumnHeaderContextMenu>                    
  10.     <!-- Add GridVeiw Columns -->                      
  11.     <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Path=Name}" />  
  12.     <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Path=Age}" />  
  13.     <GridViewColumn Header="Book" Width="250" DisplayMemberBinding="{Binding Path=Book}" />  
  14.     <GridViewColumn Header="MVP" Width="50" DisplayMemberBinding="{Binding Path=Mvp}" />  
  15.       
  16. </GridView>  
                                             Listing 3

The output of listing looks as in Figure 2.

ContextMenu
                                                                   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 a ColumnHeaderTemplate of a GridView.
  1. <Window.Resources>          
  2.     <DataTemplate x:Key="OrangeHeaderTemplate">  
  3.         <DockPanel>  
  4.             <CheckBox/>  
  5.             <TextBlock FontSize="10" Foreground="Orange"  
  6.                        FontWeight="Bold" >  
  7.       <TextBlock.Text>  
  8.         <Binding/>  
  9.       </TextBlock.Text>  
  10.             </TextBlock>  
  11.         </DockPanel>  
  12.     </DataTemplate>          
  13.       
  14. </Window.Resources>  
                                                                                                                        Listing 4

The code snippet in Listing 5 sets the ColumnHeaderTemplate of a GridView.
  1. <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="Authors"  
  2.           ColumnHeaderTemplate="{StaticResource OrangeHeaderTemplate}" >  
                                                      Listing 5

The output of Listing 5 looks as in Figure 3.

CheckBox
                                                   Figure 3

Summary

In this article, I explained how to create and use a GridView control in WPF.

Up Next
    Ebook Download
    View all
    Learn
    View all