如果GeometryGroup中只有一个几何对象,如何正确刷?

时间:2011-10-11 01:52:16

标签: wpf system.windows.media

我有一个标签(例如,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));
    }
}

将生成:

enter image description here

,而

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));
    }
}

将生成:

enter image description here

我想要的是每个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;
}

正确生成:

enter image description here

但是

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;
}

将生成:

enter image description here

其中矩形错误地放置在标签的中心。

1 个答案:

答案 0 :(得分:1)

只需要你的

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

或者使用此XAML应用于所有标签......

    <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>

希望这有帮助。