【问题标题】:UWP ListView items not filling ListView's WidthUWP ListView 项目未填充 ListView 的宽度
【发布时间】:2019-01-15 12:44:43
【问题描述】:

我有一个设置宽度为 1000 的 ListView,如果项目的宽度设置为 1000 或更大,我希望项目展开以填充此宽度。

我找到的所有答案都暗示了这一点

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </ListView.ItemContainerStyle>

什么都没做。

这是 ListView 代码。

    <ListView Width="1000" Background="Violet">

        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Width="1000" Background="Black" HorizontalAlignment="Center">
                    <TextBlock Text="{Binding}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.Items>
            <x:String>ITEM1</x:String>
            <x:String>ITEM2</x:String>
        </ListView.Items>

    </ListView>

我试过 ListBox 并且这种行为也存在。显然,这些项目可以填充原始宽度 - 24,因此,如果我将项目的宽度设置为 976,它会完美地填充容器。再多,它就会隐藏在容器后面。

谢谢。

【问题讨论】:

  • 从ItemTemplate的DataTemplate中去掉Width参数,将HorizontalAlignment都设置为Stretch而不是Center;告诉我会发生什么。我不明白“不填充 ListView 的宽度”是什么意思。你能附上你想要的情况和当前情况的照片吗?
  • @Muzib 从模板中删除宽度将其缩小到项目的最小宽度,在这种情况下,两种对齐方式实际上都没有做任何事情。 Martin 的回答是正确的,它是导致问题的默认填充。

标签: xaml listview uwp


【解决方案1】:

问题是ListViewItem 容器有一个填充,从四面八方都是 12 epx。要删除它,请将属性也设置为 0:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Padding" Value="0" />
    </Style>
</ListView.ItemContainerStyle>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多