C#XAML拉伸网格以适合listViewItem

时间:2015-08-01 12:52:40

标签: c# xaml windows-10 windows-10-mobile

我在拉伸网格以适应listViewItem时遇到问题。虽然我将Horizo​​ntalContentAlignment设置为拉伸,但由于某种原因,它总是在每一侧留下空白。这是我的应用程序的图像,紫色=网格,蓝色= listViewItem。我希望网格(紫色)适合项目的整个宽度。如果有人能告诉我这里做错了什么,我真的很感激。

enter image description here

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>

1 个答案:

答案 0 :(得分:3)

尝试为ListViewItemPresenter

设置ContentMargin="0"
  <Style TargetType="ListViewItem">
    <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="ListViewItem">
              <ListViewItemPresenter ContentMargin="0" />
         </ControlTemplate>
     </Setter.Value>
    </Setter>
  </Style>