xamlwinui-3winuiwindows-app-sdkwinui-xaml

Is it possible to have a GridView with UniformGridLayout?


ItemsRepeater Layout can be changed to UnformGridLayout which makes the items to evenly distribute in the available space.

<ItemsRepeater
    XYFocusKeyboardNavigation="Enabled"
    IsTabStop="False"
    ItemsSource="{x:Bind ItemsSource}">
    <ItemsRepeater.Layout>
        <UniformGridLayout
            ItemsStretch="Uniform"
            MinRowSpacing="12"
            MinColumnSpacing="10"
            MinItemHeight="90"
            MinItemWidth="160" />
    </ItemsRepeater.Layout>
</ItemsRepeater>

Is it possible to get the exact same behavior for GridView (I can't use ItemsRepeater as I need Item Selection also)


Solution

  • You should try the ItemsView control:

    <ItemsView
        XYFocusKeyboardNavigation="Enabled"
        IsTabStop="False"
        ItemsSource="{x:Bind ItemsSource}">
        <ItemsView.Layout>
            <UniformGridLayout
                ItemsStretch="Uniform"
                MinRowSpacing="12"
                MinColumnSpacing="10"
                MinItemHeight="90"
                MinItemWidth="160" />
        </ItemsView.Layout>
    </ItemsView>
    

    Just so you know, if you are using the ItemsView with an ItemTemplate, make sure that an ItemContainer is on the root of the DataTemplate.