使用DrawingBrush创建两条垂直线

时间:2014-10-09 03:28:10

标签: c# wpf

我正在尝试创建一个50 x 50的正方形,其中一半是矩形白色而另一半是黑色(线条垂直而不是水平)。我有以下代码,但它没有按预期填充矩形。如何使其50%白色和50%黑色?

System.Windows.Shapes.Rectangle swatch = new System.Windows.Shapes.Rectangle();
swatch.Width = 50;
swatch.Height = 50;

DrawingBrush blackBrush = new DrawingBrush();
GeometryDrawing backgroundSquare = new GeometryDrawing(System.Windows.Media.Brushes.White,null,new RectangleGeometry(new Rect(25, 0, 50, 50)));
GeometryGroup gGroup = new GeometryGroup();
gGroup.Children.Add(new RectangleGeometry(new Rect(25, 0, 100, 100)));
GeometryDrawing checkers = new GeometryDrawing(new SolidColorBrush(Colors.Black), null, gGroup);

DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);

blackBrush.Drawing = checkersDrawingGroup;

blackBrush.Viewport = new Rect(0, 0, 0.25, 0.25);
blackBrush.TileMode = TileMode.Tile;

swatch.Fill = blackBrush;

sp_Thumbnails.Children.Add(swatch);

2 个答案:

答案 0 :(得分:2)

用黑色填充矩形的一半,用白色填充一半。我修改了你的代码如下。这将创建一个矩形,其中的线条垂直分隔两个部分。

Rectangle swatch = new System.Windows.Shapes.Rectangle();
            swatch.Width = 50;
            swatch.Height = 50;

            DrawingBrush blackBrush = new DrawingBrush();
            GeometryDrawing backgroundSquare = new GeometryDrawing(Brushes.White, null,
                new RectangleGeometry(new Rect(0, 0, 25, 25)));
            GeometryGroup gGroup = new GeometryGroup();
            gGroup.Children.Add(new RectangleGeometry(new Rect(25, 0, 25, 25)));
            GeometryDrawing checkers = new GeometryDrawing(Brushes.Black, null, gGroup);

            DrawingGroup checkersDrawingGroup = new DrawingGroup();
            checkersDrawingGroup.Children.Add(backgroundSquare);
            checkersDrawingGroup.Children.Add(checkers);

            blackBrush.Drawing = checkersDrawingGroup;            
            swatch.Fill = blackBrush;

            brdrect.Children.Add(swatch);

如果您希望您的部分水平拼接,那么您需要在上面的代码中进行一些更改。 只需修改矩形图形创建部分,如下所示。

GeometryDrawing backgroundSquare = new GeometryDrawing(Brushes.White, null,
                new RectangleGeometry(new Rect(0, 0, 25, 25)));
            GeometryGroup gGroup = new GeometryGroup();
            gGroup.Children.Add(new RectangleGeometry(new Rect(0, 25, 25, 25)));
            GeometryDrawing checkers = new GeometryDrawing(Brushes.Black, null, gGroup);

答案 1 :(得分:2)

简单地让三个部分在drawingGroup中只有一个GeometryDrawing对象。 你也可以在你的drawingGroup中配置GeometryDrawing的数量,如下所示。

请参阅问题的通用解决方案,该解决方案将根据groupCount值显示水平部分。

public void CreateRectangle(int groupCount)
        {
            Rectangle swatch = new System.Windows.Shapes.Rectangle();
            swatch.Width = 50;
            swatch.Height = 50;
            double groupsize = 100 / groupCount;
            DrawingBrush blackBrush = new DrawingBrush();
            DrawingGroup checkersDrawingGroup = new DrawingGroup();
            //Considering 3 as groupCount
            List<SolidColorBrush> brushes = new List<SolidColorBrush>() { Brushes.Black, Brushes.White,Brushes.Red };
            double location = 0;
            for (int i = 0; i < groupCount; i++)
            {                
                GeometryDrawing drawing = new GeometryDrawing(brushes[i] , null,
                    new RectangleGeometry(new Rect(0, location,groupsize,groupsize)));
                checkersDrawingGroup.Children.Add(drawing);
                location += groupsize;
            }
            blackBrush.Drawing = checkersDrawingGroup;
            swatch.Fill = blackBrush;

            brdrect.Children.Add(swatch);
        }