GridView

The GridView control in WinUI provides a flexible way to display and manage collections of data in a grid-like layout. It excels at presenting tabular data with multiple columns and rows, allowing for sorting, filtering, and selection.

Introduction

The GridView is a powerful UI element for presenting structured data. It virtualizes its content, meaning it only renders the items that are currently visible, which is crucial for performance when dealing with large datasets.

Key Features

Basic Usage

Here's a simple example of how to define a GridView with columns and bind it to a collection of data:

<Page
    x:Class="WinUIDemo.Views.GridViewPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <GridView ItemsSource="{x:Bind SampleData}"
                  SelectionMode="Extended"
                  IsItemClickEnabled="True"
                  ItemClick="{x:Bind ViewModel.ItemClickedCommand}">

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>

            <GridView.ItemTemplate>
                <DataTemplate x:DataType="local:SampleDataItem">
                    <StackPanel Width="200" Height="150" Background="LightGray" Padding="10" CornerRadius="5">
                        <TextBlock Text="{x:Bind Title}" FontSize="16" FontWeight="SemiBold" Margin="0,0,0,5" />
                        <TextBlock Text="{x:Bind Description}" TextWrapping="Wrap" FontSize="12" />
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
    </Grid>
</Page>

Defining Columns

While the example above uses an ItemTemplate for a more visual grid, you can also define explicit columns similar to a DataGrid for tabular data. This is achieved using GridView.Columns:

<GridView ItemsSource="{x:Bind SampleData}">
    <GridView.Columns>
        <GridViewColumn Header="ID" Width="100" DisplayMemberBinding="{Binding Id}" />
        <GridViewColumn Header="Name" Width="*" DisplayMemberBinding="{Binding Name}" />
        <GridViewColumn Header="Category" Width="150" DisplayMemberBinding="{Binding Category}" />
    </GridView.Columns>
</GridView>
Note: When using GridView.Columns, you typically don't need an ItemTemplate as the columns define the cell content.

Data Virtualization

GridView automatically uses UI virtualization for its items. This means that only the items currently visible on screen are created and rendered in the UI tree. As the user scrolls, the GridView recycles and reuses these UI elements, significantly improving performance with large datasets.

Customization

Item Container Style

You can customize the appearance and behavior of individual grid items by setting the ItemContainerStyle property:

<GridView ...>
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Background" Value="AliceBlue"/>
            <Setter Property="CornerRadius" Value="8"/>
            <Style.Triggers>
                <Trigger Property="IsPointerOver" Value="True">
                    <Setter Property="Background" Value="LightSkyBlue"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="DodgerBlue"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </GridView.ItemContainerStyle>
    ...
</GridView>

Best Practices

Tip: For very large or complex datasets requiring advanced features like editing and data validation, consider using the DataGrid control, which is specifically designed for spreadsheet-like data manipulation.