ListView: Stretch grid in custom datatemplate to max width?
c#, windows-runtime, windows-store-apps, winrt-xaml
Solution
Try also setting `HorizontalContentAlignment` to `Stretch` in your `ItemContainerStyle/Style/Setter`.
Problem
I want to create a custom `DataTemplate` for my `ListView`. I have a simple `class Person`: ``` public class Person { public string Name{get;set;} public SolidColorBrush SomeColor{get;set;} public Person(string name){ Name = name; } } ``` I set the `ItemsSource` property of my `ListView` to a `List<Person> persons`. If I just use `DisplayMemberPath=Name` everything works fine but I would like to have a custom template to show `Name` and `SomeColor`. I created a sample `Grid` to show how I want it to look like (without any bindings, just as an example): ``` <Grid Height="50"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto" MinWidth="10"/> </Grid.ColumnDefinitions> <TextBlock Text="Patrick" Grid.Column="0" TextAlignment="Right" Margin="0,0,10,0" VerticalAlignment="Center"/> <Rectangle Fill="Green" Grid.Column="1" /> </Grid> ``` This is my try to implement my `DataTemplate` for the `ListView`: ``` <ListView x:Name="listView" ItemsSource="{Binding ElementName=pageRoot, Path=FriendList ,Mode=OneWay}"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalAlignment" Value="Stretch"/> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto" MinWidth="10"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Name}" Grid.Column="0"/> <Rectangle Fill="LimeGreen" Grid.Column="1"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> ``` The problem is now that the `Grid` does not expand to the full available width of the ListView. It is just a small `Grid` of maybe 20-30px width. After searching Google I added `ListView.ItemContainerStyle` but it did not help. Any suggestions on how to solve this?