我在拉伸网格以适应listViewItem时遇到问题。虽然我将HorizontalContentAlignment设置为拉伸,但由于某种原因,它总是在每一侧留下空白。这是我的应用程序的图像,紫色=网格,蓝色= listViewItem。我希望网格(紫色)适合项目的整个宽度。如果有人能告诉我这里做错了什么,我真的很感激。
MainPage.xaml中
<ListView x:Name="ListView" IsItemClickEnabled="True" SelectionMode="None" ItemClick="item_Clicked">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource itemStyle}"/>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Background="#FFFFD9D9">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding time}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<TextBlock Text="{Binding price}" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
App.xaml - 资源
<Application.Resources>
<Style x:Key="itemStyle" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Margin" Value="0,1,0,0"/>
<Setter Property="Background" Value="Aquamarine"/>
</Style>
答案 0 :(得分:3)
尝试为ListViewItemPresenter
设置ContentMargin="0"
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter ContentMargin="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>