Bewerken

Delen via


How to: Display Data by Using GridViewRowPresenter

This example shows how to use the GridViewRowPresenter and GridViewHeaderRowPresenter objects to display data in columns.

Example

The following example shows how to specify a GridViewColumnCollection that displays the DayOfWeek and Year of a DateTime object by using GridViewRowPresenter and GridViewHeaderRowPresenter objects. The example also defines a Style for the Header of a GridViewColumn.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
 
  <Window.Resources>

    <Style x:Key="MyHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
     <Setter Property="Background" Value="BurlyWood"/>
    </Style>

    <GridViewColumnCollection x:Key="gvcc">
      <GridViewColumn Header="Year" 
                      DisplayMemberBinding="{Binding Year}" 
                      Width="80"/>
      <GridViewColumn Header="Day"  
                      DisplayMemberBinding="{Binding DayOfWeek}" 
                      Width="80" />

    </GridViewColumnCollection>

  </Window.Resources>
  
  <StackPanel>
    <GridViewHeaderRowPresenter Name="hrp" Columns="{StaticResource gvcc}"  
                                ColumnHeaderContainerStyle=
                                 "{StaticResource MyHeaderStyle}" />

    <GridViewRowPresenter Columns="{StaticResource gvcc}" >
      <GridViewRowPresenter.Content>
        <sys:DateTime>2005/2/1</sys:DateTime>
      </GridViewRowPresenter.Content>
     </GridViewRowPresenter>
    <GridViewRowPresenter Columns="{StaticResource gvcc}" >
      <GridViewRowPresenter.Content>
        <sys:DateTime>2006/10/12</sys:DateTime>
      </GridViewRowPresenter.Content>
    </GridViewRowPresenter>
  </StackPanel>

</Window>

See also