Remove padding from GridViewItem

点点圈 提交于 2020-01-24 21:09:27

问题


Does anybody know a way to remove all the padding from GridViewItem? I'm using very small items (not too small though, I exaggerated in the picture below) and there's some padding when selecting the items which makes it good really bad and I'd like to take it out. This is what I mean:

The code for the image is this:

<GridView Margin="120,80,0,0"
                  SelectionMode="Multiple">
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel
                        Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
            <GridView.ItemContainerStyle>
                <Style TargetType="GridViewItem">
                    <Setter Property="HorizontalAlignment" Value="Center" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style>
            </GridView.ItemContainerStyle>
            <Rectangle Height="10" Width="10" Fill="Red" />
            <Rectangle Height="10" Width="10" Fill="Orange" />
            <Rectangle Height="10" Width="10" Fill="Blue" />
            <Rectangle Height="10" Width="10" Fill="Green" />
            <Rectangle Height="10" Width="10" Fill="Yellow" />
        </GridView>

Thanks in advance!


回答1:


If you extract the template for a GridViewItem - you'll see that it has some hardcoded values - 4pt Margins, 40x40 paths etc. To go around that - you would need to modify the template, but you can hard-code the item dimensions to make them smaller - just make sure you are OK with invisible selection checkmarks:

    <GridView
        Margin="120,80,0,0"
        SelectionMode="Multiple">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel
                    Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.ItemContainerStyle>
            <Style
                TargetType="GridViewItem">
                <Setter
                    Property="HorizontalAlignment"
                    Value="Center" />
                <Setter
                    Property="VerticalAlignment"
                    Value="Center" />
                <Setter
                    Property="Width"
                    Value="20" />
                <Setter
                    Property="Height"
                    Value="20" />
                <Setter
                    Property="Padding"
                    Value="0" />
                <Setter
                    Property="Margin"
                    Value="0" />
            </Style>
        </GridView.ItemContainerStyle>
        <Rectangle
            Height="10"
            Width="10"
            Fill="Red" />
        <Rectangle
            Height="10"
            Width="10"
            Fill="Orange" />
        <Rectangle
            Height="10"
            Width="10"
            Fill="Blue" />
        <Rectangle
            Height="10"
            Width="10"
            Fill="Green" />
        <Rectangle
            Height="10"
            Width="10"
            Fill="Yellow" />
    </GridView>


来源:https://stackoverflow.com/questions/10490167/remove-padding-from-gridviewitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!