I'm trying to get a ListView to stretch horizontally to fill its parent (a Grid). I defined the ListView.ItemsPanel to be of type StackPanel, with Horizontal orientation. I searched around and found that the trick to get the ListViewItems to stretch horizontally would be to set the "HorizontalContentAlignment" property to "Stretch" on the ItemContainerStyle, which I did.
Unfortunately, I still can't get my items to stretch horizontally. I thought about manually calculating the width of each ListViewItem based on the ActualWidth of the parent container, but that seems too much effort for something that should be more simple.
MainPage.xaml:
<Grid Grid.Row="1" Height="40">
<ListView ItemsSource="{x:Bind ViewModel.Numbers}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:MyClass">
<Border BorderBrush="red" BorderThickness="1">
<TextBlock Text="{x:Bind Number }" HorizontalAlignment="Center" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
MainViewModel.cs:
public class MainViewModel : ObservableRecipient
{
public List<MyClass> Numbers
{
get; set;
}
public MainViewModel()
{
Numbers = new List<MyClass>()
{
new MyClass { Number = 1 },
new MyClass { Number = 2 },
new MyClass { Number = 3 },
new MyClass { Number = 4 },
new MyClass { Number = 5 },
new MyClass { Number = 6 },
new MyClass { Number = 7 },
new MyClass { Number = 8 }
};
}
}
MyClass.cs
public class MyClass
{
public int Number
{
get; set;
}
}
The output is:
I need the numbers to occupy the horizontal available space
I'm using Win UI with the Windows App SDK 1.2.
Can you help?
(I've changed the code to a minimal reproducible case)