How to: Display ListView Contents by Using a GridView

This example shows how to define a GridView view mode for a ListView control.

Example

You can define the view mode of a GridView by specifying GridViewColumn objects. The following example shows how to define GridViewColumn objects that bind to the data content that is specified for the ListView control. This GridView example specifies three GridViewColumn objects that map to the FirstName, LastName, and EmployeeNumber fields of the EmployeeInfoDataSource that is set as the ItemsSource of the ListView control.


<ListView ItemsSource="{Binding Source={StaticResource EmployeeInfoDataSource}}">

    <ListView.View>

        <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>

    </ListView.View>
</ListView>

The following illustration shows how this example appears.

Screenshot that shows a ListView with GridView output.

See also