我有问题如何制作线而不是虚线。
我的代码:
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
答案 0 :(得分:2)
您可以创建自己的Grid
来绘制线条。
<wpfApplication5:MyGrid ShowGridLines="True">
<wpfApplication5:MyGrid.GridLinesPen>
<Pen Brush="DeepSkyBlue" Thickness="5" />
</wpfApplication5:MyGrid.GridLinesPen>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</wpfApplication5:MyGrid>
背后的代码:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApplication5
{
public class MyGrid : Grid
{
public new static readonly DependencyProperty ShowGridLinesProperty = DependencyProperty.Register(
"ShowGridLines", typeof (bool), typeof (MyGrid), new PropertyMetadata(default(bool)));
public static readonly DependencyProperty GridLinesPenProperty = DependencyProperty.Register(
"GridLinesPen", typeof (Pen), typeof (MyGrid),
new FrameworkPropertyMetadata(default(Pen), FrameworkPropertyMetadataOptions.AffectsRender));
public new bool ShowGridLines
{
get { return (bool) GetValue(ShowGridLinesProperty); }
set { SetValue(ShowGridLinesProperty, value); }
}
public Pen GridLinesPen
{
get { return (Pen) GetValue(GridLinesPenProperty); }
set { SetValue(GridLinesPenProperty, value); }
}
protected override void OnRender(DrawingContext dc)
{
Pen pen = GridLinesPen;
if (pen != null)
{
ColumnDefinitionCollection columnDefinitions = ColumnDefinitions;
if (columnDefinitions.Count > 1)
{
for (int i = 1; i < columnDefinitions.Count; i++)
{
ColumnDefinition columnDefinition = columnDefinitions[i];
double offset = columnDefinition.Offset;
dc.DrawLine(pen, new Point(offset, 0), new Point(offset, RenderSize.Height - 1));
}
}
RowDefinitionCollection rowDefinitions = RowDefinitions;
if (rowDefinitions.Count > 1)
{
for (int i = 1; i < rowDefinitions.Count; i++)
{
RowDefinition rowDefinition = rowDefinitions[i];
double offset = rowDefinition.Offset;
dc.DrawLine(pen, new Point(0, offset), new Point(RenderSize.Width - 1, offset));
}
}
}
base.OnRender(dc);
}
}
}
注意:将new
添加到ShowGridLines
我正在规避该属性的原始行为。
答案 1 :(得分:0)
您可以将每个网格元素包装成边框并根据需要设置样式
(只是这样清楚,你不会改变你的列/行的定义,保持原样,但不是把你想放在每个行/列中的任何东西放在一边,放一个边框并把内容放到里面边界)