3

我有一个Grid里面有很多按钮的。这些按钮应该是无缝连接的。在大多数情况下,这实际上是有效的,但有时网格的列/行之间会有一条白色条纹:

图像中有白色条纹

我正在通过后面的代码将按钮添加到网格中(对于非最小代码,我很抱歉,但我真的不知道这里可能有什么相关的):

public partial class MainWindow
{
    private int _xCount;
    private int _yCount;

    public MainWindow ()
    {
        InitializeComponent ();

        SetSize (40, 40);
    }

    public void SetSize (int x, int y)
    {
        _xCount = x;
        _yCount = y;

        Grid.ColumnDefinitions.Clear ();
        Grid.RowDefinitions.Clear ();

        for (var i = 0; i < x; i++)
            Grid.ColumnDefinitions.Add (new ColumnDefinition {Width = new GridLength (100, GridUnitType.Star)});
        for (var i = 0; i < y; i++)
            Grid.RowDefinitions.Add (new RowDefinition {Height = new GridLength (100, GridUnitType.Star)});

        for (var xI = 0; xI < x; xI++)
            for (var yI = 0; yI < y; yI++)
            {
                var button = new Button
                {
                    BorderThickness = new Thickness (1),
                    BorderBrush     = Brushes.Gray,
                    Foreground      = Brushes.DarkGray,
                    Content         = "",
                    Background      = Brushes.DarkGray
                };

                Grid.Children.Add (button);
                Grid.SetColumn (button, xI);
                Grid.SetRow (button, yI);
            }

        SetButtonSizes ();
    }

    private void SetButtonSizes ()
    {
        var gridWidth  = Grid.Width;
        var gridHeight = Grid.Height;

        var buttonWidth  = gridWidth / _xCount;
        var buttonHeight = gridHeight / _yCount;

        foreach (var button in Grid.Children)
        {
            ((Button) button).Width  = buttonWidth;
            ((Button) button).Height = buttonHeight;
        }
    }

    protected override void OnRenderSizeChanged (SizeChangedInfo sizeInfo)
    {
        base.OnRenderSizeChanged (sizeInfo);
        SetButtonSizes ();
    }
}

WPF 非常简单,看起来像这样:

<Window x:Class="Minesweeper.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="1000"
        Width="1000">
    <Grid Name="Grid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Window>

我已经尝试过Pixel Snapping,但没有任何区别。

4

1 回答 1

5

您应该UseLayoutRounding在 Grid 上设置为 true,而不是以编程方式调整按钮的大小。


但是,您可以通过使用 UniformGrid 大大简化您的代码

<Window ...>
    <UniformGrid x:Name="grid"/>
</Window>

并添加这样的按钮:

public MainWindow()
{
    InitializeComponent();
    SetSize(40, 40);
}

private void SetSize(int x, int y)
{
    grid.Children.Clear();
    grid.Columns = x;

    for (int i = 0; i < x * y; i++)
    {
        grid.Children.Add(new Button
        {
            BorderThickness = new Thickness(1),
            BorderBrush = Brushes.Gray,
            Background = Brushes.DarkGray,
            Foreground = Brushes.DarkGray,
            Content = ""
        });
    }
}
于 2018-06-14T11:57:53.173 回答