.net 如何更改 WPF 中网格的网格线颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/606220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:15:28  来源:igfitidea点击:

How can I change the color of the gridlines of a Grid in WPF?

.netwpf

提问by Natrium

I have a Grid(not a DataGrid, but a real Grid), with GridLinesset to True. How can I change the color of the gridlines? Hardcoded in XAML is ok, since it is just for development-reasons.

我有一个Grid(不是 DataGrid,而是一个真正的 Grid),GridLines设置为True. 如何更改网格线的颜色?在 XAML 中硬编码是可以的,因为它只是出于开发原因。

<Grid ShowGridLines="True" />

回答by jschroedl

Sorry, can't be done with ShowGridLines - you need to style the elements contained.

抱歉,无法使用 ShowGridLines 完成 - 您需要为所包含的元素设置样式。

Exhibit A:

附件A:

MSDN docs say "Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders."

MSDN 文档说“只有虚线可用,因为此属性旨在作为调试布局问题的设计工具,而不是用于生产质量代码。如果您想要网格内的线条,请将网格内的元素设置为具有边框的样式.”

Exhibit B - The WPF Source Code:

图表 B - WPF 源代码:

Notice the Brushes.Blue and Brushes.Yellow hard-coded in this sealed internal class which System.Windows.Controls.Grid uses to draw the lines.

注意 Brushes.Blue 和 Brushes.Yellow 硬编码在 System.Windows.Controls.Grid 用于绘制线条的密封内部类中。

From Grid.cs

来自 Grid.cs

    /// <summary>
    /// Helper to render grid lines. 
    /// </summary>
    internal class GridLinesRenderer : DrawingVisual 
    { 
        /// <summary>
        /// Static initialization 
        /// </summary>
        static GridLinesRenderer()
        {
            s_oddDashPen = new Pen(Brushes.Blue, c_penWidth); 
            DoubleCollection oddDashArray = new DoubleCollection();
            oddDashArray.Add(c_dashLength); 
            oddDashArray.Add(c_dashLength); 
            s_oddDashPen.DashStyle = new DashStyle(oddDashArray, 0);
            s_oddDashPen.DashCap = PenLineCap.Flat; 
            s_oddDashPen.Freeze();

            s_evenDashPen = new Pen(Brushes.Yellow, c_penWidth);
            DoubleCollection evenDashArray = new DoubleCollection(); 
            evenDashArray.Add(c_dashLength);
            evenDashArray.Add(c_dashLength); 
            s_evenDashPen.DashStyle = new DashStyle(evenDashArray, c_dashLength); 
            s_evenDashPen.DashCap = PenLineCap.Flat;
            s_evenDashPen.Freeze(); 
        }

回答by igalk474

var T = Type.GetType("System.Windows.Controls.Grid+GridLinesRenderer," +
    " PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

var GLR = Activator.CreateInstance(T);
GLR.GetType().GetField("s_oddDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(GLR, new Pen(Brushes.Black, 1.0));
GLR.GetType().GetField("s_evenDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(GLR, new Pen(Brushes.Black, 1.0));

myGrid.ShowGridLines = true;

回答by JLott

This answer is not how to actually change the GridLines, but how to make it look like you did in a very simple way. I am sure others have better ways to do this, but here is how I accomplished showing my gridlines.

这个答案不是如何实际更改 GridLine,而是如何以非常简单的方式使它看起来像您所做的那样。我相信其他人有更好的方法来做到这一点,但这里是我如何完成显示我的网格线。

<Grid Height="115" Margin="0,0,0,0" ShowGridLines="False">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="220"/>
                                    <ColumnDefinition Width="220"/>
                                    <ColumnDefinition Width="200*"/> 
                                </Grid.ColumnDefinitions>
                                <Border Grid.Column="0" BorderBrush="White" BorderThickness="1">
                                </Border>
                                <Border Grid.Column="1" BorderBrush="White" BorderThickness="1">                      
                                </Border>
                                <Border Grid.Column="2" BorderBrush="White" BorderThickness="1">        
                                </Border>
                            </Grid>

Create a border for your column definitions or grid definitions and set the Grid.Column property to whatever you wish. Then you can set your color, thickness, or style. Sweet and simple. Hope this helps someone else!

为您的列定义或网格定义创建一个边框,并将 Grid.Column 属性设置为您希望的任何内容。然后您可以设置颜色、厚度或样式。甜美而简单。希望这对其他人有帮助!

Here is an image of this grid:

这是此网格的图像:

enter image description here

在此处输入图片说明

回答by primo

An extension of igalk474's answer, used as:

igalk474's answer的扩展,用作:

<FixedPage.Resources>
  <local:GridLinesRenderer x:Key="glr" StrokeColor="Black" StrokeWidth="1.0" />
</FixedPage.Resources>

...

<Grid ShowGridLines="True">
...
</Grid>

Where localis the local assembly, and FixedPageis any FrameworkElement.

local本地程序集在哪里,FixedPage是任何FrameworkElement

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Media;

namespace mynamespace.xaml {
    public class GridLinesRenderer : DependencyObject {

        public static readonly DependencyProperty StrokeWidthProperty = DependencyProperty.Register(
            "StrokeWidth", typeof(double), typeof(GridLinesRenderer), new PropertyMetadata(1.0, OnPropertyChanged)
        );

        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
            "StrokeColor", typeof(SolidColorBrush), typeof(GridLinesRenderer), new PropertyMetadata(Brushes.Black, OnPropertyChanged)
        );

        public double StrokeWidth {
            get { return (double)GetValue(StrokeWidthProperty); }
            set { SetValue(StrokeWidthProperty, value); }
        }

        public SolidColorBrush StrokeColor {
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
            set { SetValue(StrokeColorProperty, value); }
        }

        public GridLinesRenderer() {
            OnPropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        private static void OnPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) {
            Type T = Type.GetType("System.Windows.Controls.Grid+GridLinesRenderer," +
                "PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            var glr = Activator.CreateInstance(T);
            Pen glrPen = new Pen(((GridLinesRenderer)source).StrokeColor, ((GridLinesRenderer)source).StrokeWidth);
            glr.GetType().GetField("s_oddDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(glr, glrPen);
            glr.GetType().GetField("s_evenDashPen", BindingFlags.Static | BindingFlags.NonPublic).SetValue(glr, glrPen);
        }

    }
}

回答by Ravi Shankar

<Window.Resources>
   <SolidColorBrush x:Key="RedGridLine" Color="#FFFF4444" />
   <SolidColorBrush x:Key="BlueGridLine" Color="#554444FF"/>
</Window.Resources>

<my:DataGrid VerticalGridLinesBrush="{StaticResource RedGridLine}"
        HorizontalGridLinesBrush="{StaticResource BlueGridLine}" >
...
</my:DataGrid>

Ref: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0202b0dd-38d9-4ad7-8576-d115922aeeec/

参考:http: //social.msdn.microsoft.com/Forums/en-US/wpf/thread/0202b0dd-38d9-4ad7-8576-d115922aeeec/