ListView ItemTemplate 100% 宽度 [英] ListView ItemTemplate 100% width
问题描述
如何在使用 DataTemplate
时使每个 ListView
项目的内容扩展到 100% 宽度?
How can I make content of each ListView
item expands to 100% width when using a DataTemplate
?
我在 ListView
和 HorizontalAlignment="Stretch"
中尝试了 HorizontalContentAlignment="Stretch"
在 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.
我有这样的事情:
<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>
我想在 ListView
和 ItemTemplate
之间还有一层.
I guess there is one more layer between the ListView
and the ItemTemplate
.
推荐答案
我明白了.使用 HorizontalContentAlignment
设置器设置 ListView.ItemContainerStyle
就可以了.即:
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>
这篇关于ListView ItemTemplate 100% 宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!