我有一个myLabel
大小为 60x60 的标签(例如 ),我想将其背景设置为DrawingBrush
. 我的代码:
GeometryGroup testGroup = new GeometryGroup();
testGroup.Children.Add(new RectangleGeometry(new Rect(20, 20, 10, 10)));
//testGroup.Children.Add(new RectangleGeometry(new Rect(40, 40, 10, 10)));
myLabel.Background=new DrawingBrush(
new GeometryDrawing(Brushes.Black, null, testGroup));
如果我取消注释第 3 行,它会完美运行(标签内有两个黑色方块)。但是如果testGroup
只包含一个几何对象,myLabel
就会全黑。改变FillRule
或给画笔一个非空值Pen
都不会影响结果。
那么,如何只绘制一个矩形作为标签的背景呢?谢谢~
更新:更多源代码
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
GeometryGroup testGroup = new GeometryGroup();
testGroup.Children.Add(new RectangleGeometry(new Rect(20, 20, 10, 10)));
labelArray[i, j].Background =
new DrawingBrush(
new GeometryDrawing(
Brushes.Black, null, testGroup));
}
}
将产生:
尽管
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
GeometryGroup testGroup = new GeometryGroup();
testGroup.Children.Add(new RectangleGeometry(new Rect(20, 20, 10, 10)));
testGroup.Children.Add(new RectangleGeometry(new Rect(40, 40, 10, 10)));
labelArray[i, j].Background =
new DrawingBrush(
new GeometryDrawing(
Brushes.Black, null, testGroup));
}
}
将产生:
我想要的是每个中只有一个黑色方块labelArray[i, j]
,这也是第一个片段的原始目标。
更新 2:感谢@AngelWPF 的建议:
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
GeometryGroup testGroup = new GeometryGroup();
testGroup.Children.Add(new RectangleGeometry(new Rect(-10, -10, 10, 10)));
testGroup.Children.Add(new RectangleGeometry(new Rect(10, 10, 10, 10)));
DrawingBrush brush = new DrawingBrush(
new GeometryDrawing(Brushes.Black, null, testGroup));
brush.Stretch = Stretch.None;
labelArray[i, j].Background = brush;
}
正确生成:
但
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
GeometryGroup testGroup = new GeometryGroup();
testGroup.Children.Add(new RectangleGeometry(new Rect(20, 20, 10, 10)));
DrawingBrush brush = new DrawingBrush(
new GeometryDrawing(Brushes.Black, null, testGroup));
brush.Stretch = Stretch.None;
labelArray[i, j].Background = brush;
}
将产生:
其中矩形错误地放置在标签的中心。