I have a label (say, myLabel
) with size 60x60 and I want to set its background by a DrawingBrush
. My code:
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));
If I uncomment the 3rd line, it works perfectly (with two black squares within the label). But if the testGroup
contains only one geometric object, myLabel
will be entirely black. Either changing FillRule
or giving the brush a non-null Pen
does not affect the outcome.
So, how can I draw only one rectangle, as the background of the label? Thank you~
UPDATE: More source code
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));
}
}
will generate:
while
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));
}
}
will generate:
What I want is there being only one black square in each labelArray[i, j]
, which is also the original goal of the first snippet.
UPDATE 2: Thanks for @AngelWPF's suggestion:
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;
}
properly generates:
But
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;
}
will generate:
where the rectangles are wrongly placed at the center of the label.
Just make your
Or use this XAML to apply to all labels...
Hope this helps.