GridView Class

Definition

Represents a view mode that displays data items in columns for a ListView control.

[System.Windows.Markup.ContentProperty("Columns")]
[System.Windows.StyleTypedProperty(Property="ColumnHeaderContainerStyle", StyleTargetType=typeof(System.Windows.Controls.GridViewColumnHeader))]
public class GridView : System.Windows.Controls.ViewBase, System.Windows.Markup.IAddChild
Inheritance
Attributes
Implements

Examples

The following example shows how to define a ListView control that implements a GridView as its View.

  <ListView ItemsSource="{Binding Source={StaticResource EmployeeData}, 
                         XPath=Employee}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="First Name" 
                      DisplayMemberBinding="{Binding XPath=FirstName}" />
      <GridViewColumn Header="Last Name" 
                      DisplayMemberBinding="{Binding XPath=LastName}" />
      <GridViewColumn Header="Favorite City"
                      DisplayMemberBinding="{Binding XPath=FavoriteCity}"/>
    </GridView>
  </ListView.View>
</ListView>

Remarks

The GridView class and its supporting classes provide the infrastructure to display data items that are specified for a ListView control in a series of columns. The columns have column headers, which are buttons that are derived from ButtonBase, and you can reorder the columns by using drag-and-drop operations. Note that the columns of a GridView display data and do not provide direct access to the source of the data.

To specify a GridView as the view mode for a ListView, set the View property to a GridView object.

The GridView class is derived from ViewBase. You can define custom views by inheriting from the ViewBase class that provides the supporting elements for a view. For more information, see How to: Create a Custom View Mode for a ListView.

The following illustration shows an example of a ListView that uses a GridView.

ListView with GridView output

The columns in a GridView are defined as GridViewColumn objects. In Extensible Application Markup Language (XAML), you can define GridViewColumn objects as child elements of the GridView. In code, you can add a GridViewColumn to the GridView by using the Columns property and the Add method that is defined for the Collection<T> class. Similarly, you can use other Collection<T> methods such as Remove and Insert to modify the columns in a GridView.

The following example shows how to define the columns of a GridView.


GridView myGridView = new GridView();
myGridView.AllowsColumnReorder = true; 
myGridView.ColumnHeaderToolTip = "Employee Information";

GridViewColumn gvc1 = new GridViewColumn();
gvc1.DisplayMemberBinding = new Binding("FirstName");
gvc1.Header = "FirstName";
gvc1.Width = 100;
myGridView.Columns.Add(gvc1);
GridViewColumn gvc2 = new GridViewColumn();
gvc2.DisplayMemberBinding = new Binding("LastName");
gvc2.Header = "Last Name";
gvc2.Width = 100;
myGridView.Columns.Add(gvc2);
GridViewColumn gvc3 = new GridViewColumn();
gvc3.DisplayMemberBinding = new Binding("EmployeeNumber");
gvc3.Header = "Employee No.";
gvc3.Width = 100;
myGridView.Columns.Add(gvc3);


Dim myGridView As New GridView()
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"

Dim gvc1 As New GridViewColumn()
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn()
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
myGridView.Columns.Add(gvc3)


<GridView AllowsColumnReorder="true"
          ColumnHeaderToolTip="Employee Information">

  <GridViewColumn DisplayMemberBinding=
                      "{Binding Path=FirstName}" 
                  Header="First Name" Width="100"/>
    
              <GridViewColumn DisplayMemberBinding=
                      "{Binding Path=LastName}" 
                  Width="100">
                  <GridViewColumnHeader>Last Name
                      <GridViewColumnHeader.ContextMenu>
                      <ContextMenu  MenuItem.Click="LastNameCM_Click"  
                                    Name="LastNameCM">
                          <MenuItem Header="Ascending" />
                          <MenuItem Header="Descending" />
                      </ContextMenu>
                      </GridViewColumnHeader.ContextMenu>
                  </GridViewColumnHeader>
              </GridViewColumn>

              <GridViewColumn DisplayMemberBinding=
                      "{Binding Path=EmployeeNumber}" 
                  Header="Employee No." Width="100"/>
</GridView>

To style the rows in a GridView, define a style for the ListViewItem controls in the ListView.

To add visual elements, such as a CheckBox or Button control, to a GridView, use templates or styles. For an example, see How to: Create ListViewItems with a CheckBox.

Constructors

GridView()

Initializes a new instance of the GridView class.

Fields

AllowsColumnReorderProperty

Identifies the AllowsColumnReorder dependency property.

ColumnCollectionProperty

Identifies the ColumnCollectionProperty attachedproperty.

ColumnHeaderContainerStyleProperty

Identifies the ColumnHeaderContainerStyle dependency property.

ColumnHeaderContextMenuProperty

Identifies the ColumnHeaderContextMenu dependency property.

ColumnHeaderStringFormatProperty

Identifies the ColumnHeaderStringFormat dependency property.

ColumnHeaderTemplateProperty

Identifies the ColumnHeaderTemplate dependency property.

ColumnHeaderTemplateSelectorProperty

Identifies the ColumnHeaderTemplateSelector dependency property.

ColumnHeaderToolTipProperty

Identifies the ColumnHeaderToolTip dependency property.

Properties

AllowsColumnReorder

Gets or sets whether columns in a GridView can be reordered by a drag-and-drop operation.

ColumnHeaderContainerStyle

Gets or sets the style to apply to column headers.

ColumnHeaderContextMenu

Gets or sets a ContextMenu for the GridView.

ColumnHeaderStringFormat

Gets or sets a composite string that specifies how to format the column headers of the GridView if they are displayed as strings.

ColumnHeaderTemplate

Gets or sets a template to use to display the column headers.

ColumnHeaderTemplateSelector

Gets or sets the selector object that provides logic for selecting a template to use for each column header.

ColumnHeaderToolTip

Gets or sets the content of a tooltip that appears when the mouse pointer pauses over one of the column headers.

Columns

Gets the collection of GridViewColumn objects that is defined for this GridView.

DefaultStyleKey

Gets the reference for the default style for the GridView.

GridViewItemContainerStyleKey

Gets the key that references the style that is defined for each ListViewItem in a GridView.

GridViewScrollViewerStyleKey

Gets the key that references the style that is defined for the ScrollViewer control that encloses the content that is displayed by a GridView.

GridViewStyleKey

Gets the key that references the style that is defined for the GridView.

ItemContainerDefaultStyleKey

Gets the reference to the default style for the container of the data items in the GridView.

Methods

AddChild(Object)

Adds a GridViewColumn object to a GridView.

AddText(String)

Not supported.

ClearItem(ListViewItem)

Removes all settings, bindings, and styling from a ListViewItem.

GetAutomationPeer(ListView)

Gets the AutomationPeer implementation for this GridView object.

GetColumnCollection(DependencyObject)

Gets the contents of the System.Windows.Controls.GridView.ColumnCollection attached property.

PrepareItem(ListViewItem)

Prepares a ListViewItem for display according to the definition of this GridView object.

SetColumnCollection(DependencyObject, GridViewColumnCollection)

Sets the contents of the System.Windows.Controls.GridView.ColumnCollection attached property.

ShouldSerializeColumnCollection(DependencyObject)

Determines whether to serialize the System.Windows.Controls.GridView.ColumnCollection attached property.

ToString()

Returns the string representation of the GridView object.

Equals(Object, Object) Inherited from Object
GetType() Inherited from Object
MemberwiseClone() Inherited from Object
ReferenceEquals(Object, Object) Inherited from Object
ClearValue(DependencyProperty) Inherited from DependencyObject
ClearValue(DependencyPropertyKey) Inherited from DependencyObject
CoerceValue(DependencyProperty) Inherited from DependencyObject
DependencyObjectType Inherited from DependencyObject
Equals(Object) Inherited from DependencyObject
GetHashCode() Inherited from DependencyObject
GetLocalValueEnumerator() Inherited from DependencyObject
GetValue(DependencyProperty) Inherited from DependencyObject
InvalidateProperty(DependencyProperty) Inherited from DependencyObject
IsSealed Inherited from DependencyObject
OnPropertyChanged(DependencyPropertyChangedEventArgs) Inherited from DependencyObject
ReadLocalValue(DependencyProperty) Inherited from DependencyObject
SetCurrentValue(DependencyProperty, Object) Inherited from DependencyObject
SetValue(DependencyProperty, Object) Inherited from DependencyObject
SetValue(DependencyPropertyKey, Object) Inherited from DependencyObject
ShouldSerializeProperty(DependencyProperty) Inherited from DependencyObject
CheckAccess() Inherited from DispatcherObject
Dispatcher Inherited from DispatcherObject
VerifyAccess() Inherited from DispatcherObject

Explicit Interface Implementations

IAddChild.AddChild(Object)

Adds a child object.

IAddChild.AddText(String)

Adds the text content of a node to the object.

Applies to

See Also