1.思路主要代码
wpf的gridline原本效果是虚线类型的。有时候需要设计成表格形式的,因此有了用附加属性来自动绘制边框线的想法。
思路:绘制Line并添加到grid的children里,但效果并不理想,会出现锯齿,像素对齐,模糊等问题。
UseLayoutRounding="False"
SnapsToDevicePixels="True"
RenderOptions.EdgeModeProperty 貌似都没起作用。
于是想到了用border来实现,简单又实用吧 哈哈。
大致思路如下:绘制border的左边框和上边框,在边界的时候考虑边界封闭。然后将border平移一半的距离。这样边框就居中并且包围了所有的线。
主要代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Media;
namespace
用附加属性修改Grid的边框
{
public
class
GridHelper
{
private
static
void
RefreshGrid(Grid grid,
int
lineWidth, Brush color)
{
for
(
var
i = grid.Children.Count - 1; i > 0; i--)
{
var
child = grid.Children[i];
var
bd = child
as
Border;
if
(bd !=
null
&& bd.Tag !=
null
&& bd.Tag.ToString() ==
"gridline"
)
{
grid.Children.Remove(bd);
}
}
var
rows = grid.RowDefinitions.Count;
var
cols = grid.ColumnDefinitions.Count;
//边界考虑
if
(rows == 0)
{
rows = 1;
}
if
(cols == 0)
{
cols = 1;
}
//生成行列
for
(
var
i = 0; i < rows; i++)
{
for
(
var
j = 0; j < cols; j++)
{
var
thick =
new
Thickness(lineWidth, lineWidth, 0, 0);
var
margin =
new
Thickness(-lineWidth/2d, -lineWidth/2d, 0, 0);
//边界考虑
if
(i == 0)
{
margin.Top = 0;
}
if
(i == rows - 1)
{
thick.Bottom = lineWidth;
}
if
(j == 0)
{
margin.Left = 0;
}
if
(j == cols - 1)
{
thick.Right = lineWidth;
}
var
bd =
new
Border
{
BorderThickness = thick,
Margin = margin,
BorderBrush = color,
Tag =
"gridline"
};
Grid.SetRow(bd, i);
Grid.SetColumn(bd, j);
grid.Children.Add(bd);
}
}
grid.InvalidateArrange();
grid.InvalidateVisual();
}
#region 线颜色
// Using a DependencyProperty as the backing store for LineColor. This enables animation, styling, binding, etc...
public
static
readonly
DependencyProperty LineColorProperty =
DependencyProperty.RegisterAttached(
"LineColor"
,
typeof
(Brush),
typeof
(GridHelper),
new
PropertyMetadata(Brushes.Black, LineColorPropertyChanged));
public
static
Brush GetLineColor(DependencyObject obj)
{
return
(Brush) obj.GetValue(LineColorProperty);
}
public
static
void
SetLineColor(DependencyObject obj, Brush value)
{
obj.SetValue(LineColorProperty, value);
}
private
static
void
LineColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var
grid = d
as
Grid;
if
(grid ==
null
)
{
return
;
}
var
showLines = GetShowGridLines(grid);
var
color = GetLineColor(grid);
var
lineWidth = GetLineWidth(grid);
if
(showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded +=
delegate
{ RefreshGrid(grid, lineWidth, color); };
}
}
#endregion
#region 线宽度
// Using a DependencyProperty as the backing store for LineWidth. This enables animation, styling, binding, etc...
public
static
readonly
DependencyProperty LineWidthProperty =
DependencyProperty.RegisterAttached(
"LineWidth"
,
typeof
(
int
),
typeof
(GridHelper),
new
PropertyMetadata(1, LineWidthPropertyChanged));
public
static
int
GetLineWidth(DependencyObject obj)
{
return
(
int
) obj.GetValue(LineWidthProperty)
;
}
public
static
void
SetLineWidth(DependencyObject obj,
int
value)
{
obj.SetValue(LineWidthProperty, value);
}
private
static
void
LineWidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var
grid = d
as
Grid;
if
(grid ==
null
)
{
return
;
}
var
showLines = GetShowGridLines(grid);
var
color = GetLineColor(grid);
var
lineWidth = GetLineWidth(grid);
if
(showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded +=
delegate
{ RefreshGrid(grid, lineWidth, color); };
}
}
#endregion
#region 是否显示线
// Using a DependencyProperty as the backing store for ShowGridLines. This enables animation, styling, binding, etc...
public
static
readonly
DependencyProperty ShowGridLinesProperty =
DependencyProperty.RegisterAttached(
"ShowGridLines"
,
typeof
(
bool
),
typeof
(GridHelper),
new
PropertyMetadata(
false
, ShowGridLinesPropertyChanged));
public
static
bool
GetShowGridLines(DependencyObject obj)
{
return
(
bool
) obj.GetValue(ShowGridLinesProperty);
}
public
static
void
SetShowGridLines(DependencyObject obj,
bool
value)
{
obj.SetValue(ShowGridLinesProperty, value);
}
private
static
void
ShowGridLinesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var
grid = d
as
Grid;
if
(grid ==
null
)
{
return
;
}
var
showLines = GetShowGridLines(grid);
var
color = GetLineColor(grid);
var
lineWidth = GetLineWidth(grid);
if
(showLines)
{
// grid.SnapsToDevicePixels = true;
grid.Loaded +=
delegate
{ RefreshGrid(grid, lineWidth, color); };
}
}
#endregion
}
}
|
2.效果图
效果还可以,任何分辨率下,任何边框大小,都没有出现像素对齐或者模糊问题。 图中的虚线是grid的默认gridLine,红色和绿色是自定义的gridline,跟虚线完美重合。
3.源码下载
https://files.cnblogs.com/files/chlm/%E7%94%A8%E9%99%84%E5%8A%A0%E5%B1%9E%E6%80%A7%E4%BF%AE%E6%94%B9Grid%E7%9A%84%E8%BE%B9%E6%A1%86.rar
http://www.iru8567.cn/
http://www.dyq9159.cn/
http://www.ece1729.cn/
http://www.zpp0623.cn/
http://www.ymg3874.cn/
http://www.jaf1308.cn/
http://www.djz1658.cn/
http://www.jlh2124.cn/
http://www.acj2609.cn/
http://www.per1537.cn/
http://www.jxa6372.cn/
http://www.dja2819.cn/
http://www.sml6389.cn/
http://www.hgh3722.cn/
http://www.ntd5264.cn/
http://www.esn7121.cn/
http://www.oee2689.cn/
http://www.pdh7765.cn/
http://www.qsv0141.cn/
http://www.mar4014.cn/
http://www.rcd9187.cn/
http://www.unw3900.cn/
http://www.tmo9604.cn/
http://www.eko5785.cn/
http://www.eqn5017.cn/
http://www.oek0353.cn/
http://www.hmw0652.cn/
http://www.tub1546.cn/
http://www.taj7240.cn/
http://www.dto7731.cn/
http://www.tox1106.cn/
http://www.pzx0011.cn/
http://www.kdd4058.cn/
http://www.bti6873.cn/
http://www.nvg6571.cn/
http://www.krr6354.cn/
http://www.uym8620.cn/
http://www.blm3653.cn/
http://www.uqt9445.cn/
http://www.txr3194.cn/
http://www.hqu4552.cn/
http://www.veo6686.cn/
http://www.usx0758.cn/
http://www.bpl6646.cn/
http://www.iyi5365.cn/
http://www.xgm6868.cn/
http://www.hfk7896.cn/
http://www.ngp3761.cn/
http://www.bwb3762.cn/
http://www.jbx0190.cn/
http://www.fvj9657.cn/
http://www.ife7579.cn/
http://www.zws1014.cn/
http://www.bou1446.cn/
http://www.xvn7640.cn/
http://www.zus2206.cn/
http://www.muu1638.cn/
http://www.sxw8975.cn/