Skip to content

ListView with inner Grid does not respect ColumnDefinition width on Windows 10 #10300

@carlos-zamora

Description

@carlos-zamora

Describe the bug

On Windows 10, if I have the following setup, the grid's column definitions aren't respected:

<ListView ItemsSource="{x:Bind Items}" Width="300">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{x:Bind}" VerticalAlignment="Center"/>
                <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button Content="Btn"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Specifically, note the star-sizing on the ColumnDefinitions.

Steps to reproduce the bug

  1. Add the following code to your XAML
<ListView ItemsSource="{x:Bind Items}" Width="300">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{x:Bind}" VerticalAlignment="Center"/>
                <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
                    <Button Content="Btn"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
  1. Make sure Items exists! I just made it a simple IVector<String>

Expected behavior

Image

Note: this image was taken on Windows 11, where the issue doesn't occur.

I also found that Grids on their own (not inside a ListView) are fine.

Screenshots

Image

It seems like the ColumnDefinition.Width is set to Auto regardless of the value I put in. I was hoping that setting HorizontalAlignment to Right would remedy the issue, but no luck.

NuGet package version

WinUI 2 - Microsoft.UI.Xaml 2.8.2
Microsoft.UI.Xaml 2.8.7-prerelease.241119001

Windows version

Windows 11 (22H2): Build 22621

Known workaround

I managed to work around this issue by setting the width of the first column to the max width used by my controls/app. Since the second column has to have enough space to be drawn, the first column just gets "clipped" appropriately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ListsListView, GridView, ListBox, etcbugSomething isn't workingproduct-winui2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions