Creating line pattern in code behind with WPF

烈酒焚心 提交于 2019-12-11 19:34:44

问题


I'm trying to convert the following XAML code into code behind version:

<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
              Viewport="0,0,30,30" ViewportUnits="Absolute"
              Viewbox="0,0,30,30" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Pen>
                <Pen Brush="Black" Thickness="5"/>
            </GeometryDrawing.Pen>
            <GeometryDrawing.Geometry>
                <Geometry>M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45</Geometry>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

this is what I did:

private DrawingBrush GetDBrush()
{
    DrawingBrush d = new DrawingBrush() { TileMode = TileMode.Tile, Viewport = new Rect(0, 0, 30, 0), ViewportUnits = BrushMappingMode.Absolute, Viewbox = new Rect(0, 0, 30, 0), ViewboxUnits = BrushMappingMode.Absolute };
    Brush penBrush = new SolidColorBrush(Colors.Black);
    penBrush.Freeze();
    Pen pen = new Pen(penBrush, 0.5); pen.Freeze();
    Rect r = new Rect(0, 0, 100, 30);

    Geometry g = new RectangleGeometry(r);
    GeometryDrawing drawing = new GeometryDrawing(new SolidColorBrush(Colors.BlueViolet), pen, g);
    d.Drawing = drawing;
    var dGroup = new DrawingGroup();
    using (DrawingContext dc = dGroup.Open())
    {
        dc.DrawGeometry(penBrush, null, Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45"));
    }

    return d;
}

I'm not able to set a background color, basically I would a white background with black bended lines. what's wrong with my code?


回答1:


An exact copy of the DrawingBrush declared in XAML would be this:

private DrawingBrush GetDBrush()
{
    return new DrawingBrush
    {
        TileMode = TileMode.Tile,
        Viewport = new Rect(0, 0, 30, 30),
        ViewportUnits = BrushMappingMode.Absolute,
        Viewbox = new Rect(0, 0, 30, 30),
        ViewboxUnits = BrushMappingMode.Absolute,
        Drawing = new GeometryDrawing
        {
            Pen = new Pen(Brushes.Black, 5),
            Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
        }
    };
}

In order to also have a background color, you need another Drawing:

private DrawingBrush GetDBrush()
{
    var backgroundDrawing = new GeometryDrawing
    {
        Brush = Brushes.Yellow,
        Geometry = new RectangleGeometry(new Rect(0, 0, 45, 45))
    };

    var foregroundDrawing = new GeometryDrawing
    {
        Pen = new Pen(Brushes.Black, 5),
        Geometry = Geometry.Parse("M0,0 L30,30 M15,-15 L45,15 M-15,15 L15,45")
    };

    var drawing = new DrawingGroup();
    drawing.Children.Add(backgroundDrawing);
    drawing.Children.Add(foregroundDrawing);

    return new DrawingBrush
    {
        TileMode = TileMode.Tile,
        Viewport = new Rect(0, 0, 30, 30),
        ViewportUnits = BrushMappingMode.Absolute,
        Viewbox = new Rect(0, 0, 30, 30),
        ViewboxUnits = BrushMappingMode.Absolute,
        Drawing = drawing
    };
}


来源:https://stackoverflow.com/questions/57674799/creating-line-pattern-in-code-behind-with-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!