简体   繁体   English

WPF网格白色条纹

[英]WPF Grid white stripes

I have a Grid with a lot of buttons inside. 我有一个内置很多按钮的Grid These buttons are supposed to be seamlessly connecting. 这些按钮应该是无缝连接的。 In most of the cases, this is actually working, but sometimes there's a white stripe between the columns / rows of the grid: 在大多数情况下,这实际上是有效的,但有时在网格的列/行之间有一个白色条纹:

图像中的白色条纹

I'm adding the buttons to the grid via the code behind like this (I'm sorry for non-minimal code, but I really don't know what might be relevant here): 我正在通过后面的代码将按钮添加到网格中(我很抱歉非最小代码,但我真的不知道这里可能有什么相关性):

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 ();
    }
}

The WPF is pretty trivial and looks like that: 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>

I already tried Pixel Snapping , which didn't make any difference. 我已经尝试了Pixel Snapping ,它没有任何区别。

You should set UseLayoutRounding to true on the Grid and not programmatically resize the Buttons. 您应该在网格上将UseLayoutRounding设置为true,而不是以编程方式调整按钮的大小。


However, you could greatly simplify your code by using a UniformGrid 但是,您可以使用UniformGrid大大简化代码

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

and add Buttons like this: 并添加像这样的按钮:

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 = ""
        });
    }
}

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

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