Question

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:

enter image description here

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:

enter image description here

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:

enter image description here

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:

enter image description here

where the rectangles are wrongly placed at the center of the label.

Was it helpful?

Solution

Just make your

   DrawingBrush.Stretch="None"
   DrawingBrush.ViewboxUnits="Absolute"
   DrawingBrush.Viewbox = new Rect(0,0,60,60);

Or use this XAML to apply to all labels...

    <Style TargetType="{x:Type Label}">
       <Setter Property="Background">
          <Setter.Value>
             <DrawingBrush Stretch="None" ViewboxUnits="Absolute" >
                 <DrawingBrush.Viewbox>
                     <Rect X="0" Y="0" Height="60" Width="60"/>
                 </DrawingBrush.Viewbox>
                 <DrawingBrush.Drawing>
                   <DrawingGroup>
                     <GeometryDrawing Brush="Black">
                       <GeometryDrawing.Geometry>
                         <RectangleGeometry>
                           <RectangleGeometry.Rect>
                              <Rect X="20" Y="20" Width="10" Height="10"/>
                           </RectangleGeometry.Rect>
                         </RectangleGeometry>
                       </GeometryDrawing.Geometry>
                     </GeometryDrawing>
                     <!--<GeometryDrawing Brush="Black">
                       <GeometryDrawing.Geometry>
                         <RectangleGeometry>
                           <RectangleGeometry.Rect>
                             <Rect X="40" Y="40" Width="10" Height="10"/>
                           </RectangleGeometry.Rect>
                         </RectangleGeometry>
                       </GeometryDrawing.Geometry>
                     </GeometryDrawing>-->                                        
                 </DrawingGroup>
              </DrawingBrush.Drawing>
           </DrawingBrush> 
        </Setter.Value>
      </Setter>
    </Style>

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top