C# XAML 拉伸网格以适合 listViewItem
C# XAML stretch grid to fit listViewItem
我在拉伸网格以适合 listViewItem 时遇到问题。尽管我将 HorizontalContentAlignment 设置为拉伸,但由于某种原因它总是在每一侧留空 space。这是我的应用程序的图像,紫色 = 网格,蓝色 = 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>
尝试为 ListViewItemPresenter
设置 ContentMargin="0"
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter ContentMargin="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我在拉伸网格以适合 listViewItem 时遇到问题。尽管我将 HorizontalContentAlignment 设置为拉伸,但由于某种原因它总是在每一侧留空 space。这是我的应用程序的图像,紫色 = 网格,蓝色 = 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>
尝试为 ListViewItemPresenter
设置ContentMargin="0"
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter ContentMargin="0" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>