GridView GridView GridView GridView Class

Definition

Represents a control that displays data items in rows and columns.

public : class GridView : ListViewBase, IGridViewpublic class GridView : ListViewBase, IGridViewPublic Class GridView Inherits ListViewBase Implements IGridView// This API is not available in Javascript.
<GridView .../>
-or-
<GridView ...>
  oneOrMoreItems
</GridView>
Inheritance
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Inherited Members

Inherited properties

Inherited events

Inherited methods

Examples

Here, a GridView is bound to a grouped CollectionViewSource named cvsProjects. The appearance of individual items in each group is defined by the ItemTemplate. The ItemsPanel specifies how the groups are arranged in the GridView. The GroupStyle.Panel specifies how individual items are arranged within each group. The GroupStyle.ContainerStyle is used to add a border around each group, and set its minimum size and margins. The HidesIfEmpty property is set to true to hide any empty groups.

<GridView ItemsSource="{Binding Source={StaticResource cvsProjects}}" 
  Margin="0,120,0,0" MaxHeight="500" Grid.Column="1">
    <GridView.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="20">
                <TextBlock Text="{Binding Name}" FontWeight="Bold" 
                           Style="{StaticResource BaseTextBlockStyle}"/>
                <TextBlock Text="{Binding DueDate}" TextWrapping="NoWrap" 
                           Style="{StaticResource BodyTextBlockStyle}" />
                <CheckBox Content="Complete" IsChecked="{Binding Complete}" 
                          IsEnabled="False"/>
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid MaximumRowsOrColumns="3"/>
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>

    <GridView.GroupStyle>
        <GroupStyle HidesIfEmpty="True">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <Grid Background="LightGray" Margin="0">
                        <TextBlock Text='{Binding Name}' 
                                   Foreground="Black" Margin="30"
                                   Style="{StaticResource HeaderTextBlockStyle}"/>
                    </Grid>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>

        </GroupStyle>
    </GridView.GroupStyle>
</GridView>
<GridView>
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Border Background="LightGray" Height="200" Width="200">
                    <TextBlock Text="{Binding}" 
                               FontSize="48" Foreground="Green"/>
                </Border>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.Items>
        <x:String>One</x:String>
        <GridViewItem>Two</GridViewItem>
    </GridView.Items>
</GridView>

Remarks

Use a GridView to display a collection of items in rows and columns that can scroll vertically. Data is stacked horizontally until it fills the columns, then continues with the next row. It's often used when you need to show a rich visualization of each item that takes more space, such as a photo gallery.

Grid view control

GridView is an ItemsControl, so it can contain a collection of items of any type. To populate the view, add items to the Items collection, or set the ItemsSource property to a data source.

By default, a data item is displayed in the GridView as the string representation of the data object it's bound to. To specify exactly how items in the GridView are displayed, you create a DataTemplate to define the layout of controls used to display an individual item. The controls in the layout can be bound to properties of a data object, or have content defined inline. You assign the DataTemplate to the ItemTemplate property of the GridView. For common templates you can use in your app, see Item templates for grid layouts.

Note

If you populate the GridView by setting the ItemsSource property, the ItemTemplate is applied to every item. If you populate the Items collection directly, the ItemTemplate is applied only if the item is not a GridViewItem. See Examples for more info.

If you use the GridView to display large sets of data, see Optimize ListView and GridView for tips to maintain a smooth and responsive user experience.

Prerelease. Fall Creators Update (Windows 10 Insider Preview Build 16215 and later) - Behavior change
By default, instead of performing selection, an active pen now scrolls/pans a list in UWP apps (like touch, touchpad, and passive pen). If your app depends on the previous behavior, you can override pen scrolling and revert to the previous behavior. See the Scroll​Viewer class reference for details.

By default, a user can select a single item in a GridView. You can set the SelectionMode property to a ListViewSelectionMode enumeration value to allow multi-selection or to disable selection. You can also change the GridView interaction mode to make items respond to a user click like a button instead of being selected.

This table shows the ways a user can interact with a GridView, and how you can respond to the interaction.

To enable this interaction:Use these settings:Handle this event:Use this property to get the selected item:
No interactionSelectionMode = None, IsItemClickEnabled = FalseN/AN/A
Single selectionSelectionMode = Single, IsItemClickEnabled = FalseSelectionChanged SelectedItem, SelectedIndex
Contiguous multi-selectionSelectionMode = Multiple, IsItemClickEnabled = FalseSelectionChanged SelectedItems
Non-contiguous multi-selectionSelectionMode = Extended, IsItemClickEnabled = FalseSelectionChanged SelectedItems
ClickSelectionMode = None, IsItemClickEnabled = TrueItemClick N/A

Note

The PointerWheelChanged event does not bubble up from a GridView. This means that a control that has a GridView inside of it does not receive mouse wheel change messages if the pointer is over the GridView. For example, if you put a GridView inside of a ScrollViewer, you can't scroll the ScrollViewer with the mouse wheel when the pointer is over the GridView.

Note

When you set the Padding property on a GridView control, the value for right padding is ignored; only the values for left, top, and bottom padding are applied.

GridView supports data virtualization to improve performance with large data sets. Random access virtualization is supported when the data source implements the appropriate interfaces, which vary depending on the programming language:

Windows 8 In Windows 8, when the data item in a selected GridViewItem is replaced, the SelectedIndex value is not cleared. In Windows 8.1, the SelectedIndex value is cleared.

GridView implements the ISemanticZoomInformation interface, so it can be used as a view in a SemanticZoom control. When it's used in a SemanticZoom control, always set the ScrollViewer.IsHorizontalScrollChainingEnabled attached property to false on the ScrollViewer that's in the GridView 's control template, like this: <GridView ScrollViewer.IsHorizontalScrollChainingEnabled="False">. These members have an effect only when the GridView is hosted in a SemanticZoom control: IsActiveView, IsZoomedInView, SemanticZoomOwner, CompleteViewChange, CompleteViewChangeFrom, CompleteViewChangeTo, InitializeViewChange, MakeVisible, StartViewChangeFrom, StartViewChangeTo.

The default template for GridView includes existing transition animations. Specifically, the default ItemContainerTransitions value already contains values for AddDeleteThemeTransition, ContentThemeTransition, ReorderThemeTransition and EntranceThemeTransition (with IsStaggeringEnabled="False"). If you are setting a new value for ItemContainerTransitions on GridView, consider reproducing these same theme animations as a starting point. If you are setting the property yourself, those defaults get overwritten unless you include them again in your definition.

Selection behavior and CollectionViewSource

List controls that derive from Selector have a default selection behavior that depends on what the items source is (the type that's used for ItemsSource ). If the items source is a CollectionViewSource instance, then the behavior in the selection control is that the selection will default to the current item. When the list is first displayed, the selection defaults to the first item as current item. If you don't want the first item to be selected in this case, set IsSynchronizedWithCurrentItem to false in the GridView.

Constructors

GridView() GridView() GridView() GridView()

Initializes a new instance of the GridView class.

public : GridView()public GridView()Public Sub New()// This API is not available in Javascript.

See Also