如何在WPF中更改网格的网格线的颜色? [英] How can I change the color of the gridlines of a Grid in WPF?
问题描述
我有一个 Grid
(不是DataGrid,而是真实的网格),并且 GridLines
设置为 True
。如何更改网格线的颜色?在XAML中采用硬编码的
可以,因为它只是出于开发原因。
I have a Grid
(not a DataGrid, but a real Grid), with GridLines
set to True
. How can I change the color of the gridlines?
Hardcoded in XAML is ok, since it is just for development-reasons.
<Grid ShowGridLines="True" />
推荐答案
抱歉,ShowGridLines无法完成-您需要对包含的元素进行样式设置。
Sorry, can't be done with ShowGridLines - you need to style the elements contained.
附件A:
MSDN文档说只有虚线是可用的,因为该属性旨在用作调试布局问题的设计工具,并且不适用于生产质量代码。如果要在Grid中使用线,请对Grid中的元素设置边框样式。
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."
附件B-WPF源代码:
注意画笔。黄色,在此密封的内部类中硬编码。System.Windows.Controls.Grid用于绘制线条。
Notice the Brushes.Blue and Brushes.Yellow hard-coded in this sealed internal class which System.Windows.Controls.Grid uses to draw the lines.
从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();
}
这篇关于如何在WPF中更改网格的网格线的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!