我有一个Grid
(不是DataGrid,而是真正的Grid),其中GridLines
设置为True
。如何更改网格线的颜色?在XAML中硬编码是可以的,因为这只是为了开发目的。
<Grid ShowGridLines="True" />
抱歉,使用ShowGridLines无法实现 - 您需要样式化包含的元素。
示例A:
MSDN文档称:“仅提供虚线,因为此属性旨在作为调试布局问题的设计工具,并不适用于生产质量代码。如果要在Grid内部创建线,请将Grid内的元素样式化为具有边框。”
示例B - WPF源代码:
请注意,在这个密封的内部类中硬编码了Brushes.Blue和Brushes.Yellow,System.Windows.Controls.Grid使用此类来绘制线条。
来自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();
}
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;
这个答案并不是真正改变网格线的方法,而是通过一种非常简单的方式使其看起来像是已经改变了。我相信其他人有更好的做法,但这是我展示网格线的方法。
<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>
<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>
参考:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/0202b0dd-38d9-4ad7-8576-d115922aeeec/
这是igalk474的答案的扩展,用于:
<FixedPage.Resources>
<local:GridLinesRenderer x:Key="glr" StrokeColor="Black" StrokeWidth="1.0" />
</FixedPage.Resources>
...
<Grid ShowGridLines="True">
...
</Grid>
其中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);
}
}
}