Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

wpf

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" /> 
like image 592
Natrium Avatar asked Mar 03 '09 12:03

Natrium


People also ask

How do you modify grid lines?

Select the worksheets for which you want to change the gridline color. Click File > Excel > Options. In the Advanced category, under Display options for this worksheet, make sure that the Show gridlines check box is selected. In the Gridline color box, click the color you want.

How do I show grid lines in WPF?

First Method: By typing XAML Code ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True. GridLines are helpful during debugging for determining which element is in which cell.

What is the difference between grid and DataGrid in WPF?

A Grid is a control for laying out other controls on the form (or page). A DataGrid is a control for displaying tabular data as read from a database for example.

What is Grid panel WPF?

A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties. By default, a Grid panel is created with one row and one column.


2 Answers

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

Exhibit 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."

Exhibit B - The WPF Source Code:

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

From 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();          } 
like image 86
jschroedl Avatar answered Sep 16 '22 13:09

jschroedl


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; 
like image 33
igalk474 Avatar answered Sep 17 '22 13:09

igalk474