Create a grid in a ListView ItemTemplate

I have a ListView that has a grid-based data template. Xaml is as follows:

<ListView ItemsSource="{Binding SomeItemSource}" HorizontalAlignment="Stretch" Height="281"> <ListView.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch" Margin="3" Width="Auto"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="70"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <!-- Grid content here --> <TextBlock Text="SomeTextbox" Margin="5,5,0,0"/> <TextBox Grid.Column="1" Margin="0,5,0,0" Text="{Binding SomeProperty, Mode=TwoWay}" HorizontalAlignment="Left" Width="90"/> <TextBlock Text="AnotherText" Grid.Column="0" Grid.Row="1" Margin="5,5,0,0"/> <TextBox Grid.Column="1" Grid.Row="1" Margin="0,5,0,0" Text="{Binding AnotherProperty, Mode=TwoWay}" HorizontalAlignment="Stretch" Width="300"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> 

I want the grid to fill (stretch) the entire width of the horizontal parent ListView, however, it currently wraps around the width of the sum of its contents. How can I achieve the desired behavior?

+8
c # listview wpf grid datatemplate
source share
2 answers

You need to set ListViewItem.HorizontalContentAlignment in Stretch . Try adding this to your ListView definition:

 <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListView.ItemContainerStyle> 
+17
source share
  <ListView Name="Husam_Copy" HorizontalAlignment="Left" Height="181" Margin="60,86,0,0" VerticalAlignment="Top" Width="189" > <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="Padding" Value="0"/> <Setter Property="Margin" Value="0"/> <Setter Property="BorderThickness" Value="0"/> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate> <Button Content="{Binding Title}" HorizontalAlignment="Stretch" Padding="0" Margin="0" /> </DataTemplate> </ListView.ItemTemplate> </ListView> 
+1
source share

All Articles