Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UWP Xaml GridViewItem:删除边距_Xaml_Winrt Xaml_Uwp - Fatal编程技术网

UWP Xaml GridViewItem:删除边距

UWP Xaml GridViewItem:删除边距,xaml,winrt-xaml,uwp,Xaml,Winrt Xaml,Uwp,我想创建一个网格的图像之间没有任何空白。我试图通过GridView实现这一点。如果有更简单的方法,请告诉我 我在StackOverflow上找到了以下答案: 最后得到了这个代码: 你好! 但仍有差距: 我也尝试过使用负边距,但当我使用它们时,点击不再被正确识别 如何删除边距?GridViewItem的默认最小宽度为44px 您可以通过GridViewItemStyle将其设置为0: <Setter Property="MinWidth" Value="0" />

我想创建一个网格的图像之间没有任何空白。我试图通过
GridView
实现这一点。如果有更简单的方法,请告诉我

我在StackOverflow上找到了以下答案:

最后得到了这个代码:


你好!

但仍有差距:

我也尝试过使用负边距,但当我使用它们时,点击不再被正确识别


如何删除边距?

GridViewItem的默认最小宽度为44px

您可以通过GridViewItemStyle将其设置为0:

<Setter Property="MinWidth" Value="0" />


编辑:它还有一个默认的最小高度,您可能还想将其设置为0。

下面的代码应该可以工作:


您的代码很好。如果在DataTemplate中添加网格(带有背景),您将看到网格之间没有边距:

<DataTemplate x:DataType="data:Field">
    <Grid Background="Red">
        <Image Width="20" Height="20" Margin="0" Source="{x:Bind ImagePath}"/>
    </Grid>
</DataTemplate>

您的问题如下:您为图像控件设置了一个固定大小,其中所有图像都在一个容器中,但容器没有固定大小,因此图像在容器中居中

我看到两种解决办法。首先是删除图像控件的固定大小,并使用Stretch属性正确填充:

<DataTemplate x:DataType="data:Field">
    <Image VerticalAlignment="Stretch" 
           HorizontalAlignment="Stretch" 
           Stretch="UniformToFill" 
           Margin="0" 
           Source="{x:Bind ImagePath}"/>
</DataTemplate>

对于第一种解决方案,必须确保容器的填充行为。您可以执行以下操作:

<GridViewItemPresenter ContentMargin="0"
                       Padding="0"
                       Margin="0"
                       BorderThickness="0"
                       HorizontalAlignment="Stretch"
                       HorizontalContentAlignment="Stretch" />

第二种解决方案是直接在容器上设置固定大小:

<Style x:Key="MyGridViewItemStyle"
       TargetType="GridViewItem">
    <Setter Property="Padding"
            Value="0" />
    <Setter Property="Margin"
            Value="0" />
    <Setter Property="BorderThickness"
            Value="0" />
    <Setter Property="Width"
            Value="20" />
    <Setter Property="Height"
            Value="20" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridViewItem">
                <GridViewItemPresenter ContentMargin="0"
                                       Padding="0"
                                       Margin="0"
                                       BorderThickness="0"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>


这是图像本身的边距吗?@d.moncada不,不是。不知道你为什么会被否决——这对我来说非常有效。感谢您抽出时间添加您的答案。这不适用于最小高度44以下的项目。如果不将MinHeight设置为0,仍然会有顶部和底部的间距。我尝试了1000种不同的方法,直到看到这个答案。伟大的默认情况下,MinWidth应为0!
<Style x:Key="MyGridViewItemStyle"
       TargetType="GridViewItem">
    <Setter Property="Padding"
            Value="0" />
    <Setter Property="Margin"
            Value="0" />
    <Setter Property="BorderThickness"
            Value="0" />
    <Setter Property="Width"
            Value="20" />
    <Setter Property="Height"
            Value="20" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridViewItem">
                <GridViewItemPresenter ContentMargin="0"
                                       Padding="0"
                                       Margin="0"
                                       BorderThickness="0"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>