UWP XAML:无法将GridView项拉伸到全宽

时间:2018-09-07 16:05:55

标签: uwp-xaml

我所见的关于此问题的所有解释都不适用于我的情况,其中大多数甚至都不适用于UWP XAML。

如何获取GridView网格项容器以拉伸到整个宽度? (请参阅XAML中的“ Stretch this ...”注释。)

PS:将GridView更改为ListView并没有任何改变。

XAML:

<Grid>
    <GridView x:Name="barGrid" ItemsSource="{x:Bind projects}" Margin="10" HorizontalAlignment="Stretch" 
              HorizontalContentAlignment="Stretch" Background="Pink">
        <GridView.ItemTemplate>
            <DataTemplate x:Name="DoubleBars" x:DataType="local:Project">
                <!-- **Stretch this to the max container width!** -->
                <Grid HorizontalAlignment="Stretch" Background="Orange">
                    ...
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>

        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid Orientation="Vertical" HorizontalAlignment="Left"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>

    </GridView>
</Grid>

1 个答案:

答案 0 :(得分:1)

秘密之处在于ListViewItem的样式定义

<ListView x:Name="barGrid" 
      ItemsSource="{x:Bind projects}"
      Margin="10" 
      HorizontalAlignment="Stretch"
      HorizontalContentAlignment="Stretch" 
      Background="Pink">
<ListView.Resources>
    <Style Target="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.Resources>
<ListView.ItemTemplate>
    <DataTemplate x:Name="DoubleBars" x:DataType="local:Project">
        <!-- **Stretch this to the max container width!** -->
        <Grid HorizontalAlignment="Stretch" Background="Orange">
            ...
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>