简体   繁体   English

uwp列表视图项目永远不会覆盖可用的水平空间

[英]uwp list view item never covers available horizontal space

I have a horizontal listview and I have set all sorts of stretch properties but the list view item remains in the center leaving some space on right and left side of the actual data template grid. 我有一个水平的listview,并且设置了各种拉伸属性,但是listview项保留在中心,在实际数据模板网格的左右两侧留有一些空间。 I have also noticed same behaviour on vertical listview. 我也注意到垂直列表视图上的相同行为。

在此处输入图片说明

as you can see in the image above the gap is there and is used by the listviewitem reveal highlight. 正如您在上方图像中所看到的那样,该间隙在那里,并且由listviewitem用来显示突出显示。 I want to remove this gap. 我想消除这一差距。

Code

<ListView Style="{StaticResource PivotListViewStyle}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid  Style="{StaticResource TileGridStyle}" >
                <-- other irrelivant xaml-->
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Styles 样式

<Style TargetType="ListView" x:Key="StretchListviewStyle">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="VerticalAlignment" Value="Stretch"/>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="ListView" x:Key="PivotListViewStyle" BasedOn="{StaticResource StretchListviewStyle}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <ItemsStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>        
</Style>

<Style x:Key="TileGridStretchStyle" TargetType="Grid">
    <Setter Property="Height" Value="{StaticResource MainItemHeight}"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Padding" Value="4"/>
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="{ThemeResource SystemAccentColor}"/>
        </Setter.Value>
    </Setter>
</Style>

<x:Double x:Key="MainItemHeight">114</x:Double>

<Style x:Key="TileGridStyle" TargetType="Grid"  BasedOn="{StaticResource TileGridStretchStyle}">
    <Setter Property="Width" Value="{StaticResource MainItemHeight}"/>
</Style>

Repro 摄制

to reproduce the issue you can see this minimal project : https://github.com/touseefbsb/ListViewItemSpacingBug 重现该问题,您可以看到这个最小的项目: https : //github.com/touseefbsb/ListViewItemSpacingBug

This is because the default Padding of a ListViewItem is 12,0,12,0 . 这是因为ListViewItem的默认Padding12,0,12,0

Override the padding with another value: 用另一个值覆盖填充:

    <Style TargetType="ListView" x:Key="StretchListviewStyle">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    <Setter Property="VerticalContentAlignment" Value="Stretch" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="VerticalAlignment" Value="Stretch" />
                    <Setter Property="Padding" Value="1"/>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

And this is what you get: 这就是您得到的:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM