How can I make content of each ListView
item expands to 100% width when using a DataTemplate
?
I have tried HorizontalContentAlignment="Stretch"
in the ListView
and HorizontalAlignment="Stretch"
in the DataTemplate
, but nothing seems to work, content is still aligned to the left.
I have something like this:
<ListView x:Name="questionsView" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="BlueViolet" HorizontalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch">
<TextBlock Text="{Binding}" />
<TextBlock HorizontalAlignment="Right">16 minutes ago</TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I guess there is one more layer between the ListView
and the ItemTemplate
.
Use the ItemTemplate property to define a custom user interface (UI) to display the data items. The ItemTemplate template is required by the ListView control. It usually contains controls to display the field values of a record.
To specify the custom template declaratively, add an ItemTemplate element inside the ListView control. You can then add the contents of the template to the ItemTemplate element. To display the field values of the data source that is bound to the control, use a data-binding expression.
ListView with items not filling full width. It should be noted that despite <Grid HorizontalAlignment=”Stretch”>, that produces no effect to make the items occupied the entire width of the list. The solution is to set a style to the ItemContainerStyle for the ListViewItem with the HorizontalContentAlignment=”Stretch”, as exemplified below:
The ListView control does not have any named parts. When you create a ControlTemplate for a ListView, your template might contain an ItemsPresenter within a ScrollViewer. (The ItemsPresenter displays each item in the ListView; the ScrollViewer enables scrolling within the control).
I got it. Setting the ListView.ItemContainerStyle
with a HorizontalContentAlignment
setter makes the trick. I.e.:
<ListView x:Name="questionsView" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="BlueViolet">
<Grid HorizontalAlignment="Stretch" Margin="0">
<TextBlock Text="{Binding}" />
<TextBlock HorizontalAlignment="Right">16 minutes ago</TextBlock>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Set the item container's property of HorizontalContentAlignment to Stretch, try this
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With