代码之家  ›  专栏  ›  技术社区  ›  MetaColon

WPF网格白色条纹

  •  2
  • MetaColon  · 技术社区  · 6 年前

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

    white stripes in image

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

    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 ,这没什么区别。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Clemens    6 年前

    你应该设定 UseLayoutRounding 在网格上为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 = ""
            });
        }
    }