0

我有一个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;
}

将产生:

在此处输入图像描述

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

4

1 回答 1

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>

希望这可以帮助。

于 2011-10-11T11:12:58.757 回答