通过背后的代码创建VisualBrush

时间:2018-08-08 09:40:24

标签: c# wpf visualbrush

我有这个示例作为XAML:

<VisualBrush x:Key="HatchBrush" TileMode="Tile" Viewport="0,0,5,5" ViewportUnits="Absolute" Viewbox="0,0,5,5" ViewboxUnits="Absolute" po:Freeze="True">
<VisualBrush.Visual>
<Path Data="M 0 5 L 5 0 M -2 2 L 2 -2 M 3 7 L 7 3"
    Stroke="#80ffffff" StrokeEndLineCap="Square"
    RenderOptions.EdgeMode="Aliased" />
</VisualBrush.Visual>

我需要在后面的代码中写同样的东西,但是我只能这样做:

            VisualBrush vb = new VisualBrush();
        vb.Viewport = new Rect(0, 0, 5, 5);
        vb.TileMode = TileMode.Tile;

老实说,我不知道如何编写路径数据。我该怎么办?

1 个答案:

答案 0 :(得分:0)

你可以写

vb.Visual = new Path
{
    Data = Geometry.Parse("M 0 5 L 5 0 M -2 2 L 2 -2 M 3 7 L 7 3"),
    Stroke = new SolidColorBrush(Color.FromArgb(0x80, 0xff, 0xff, 0xff))
};

但是,您根本不需要使用VisualBrush。

下面的XAML显示了如何使用带有两个图形的DrawingBrush在纯色背景上获得填充图案。

<DrawingBrush x:Key="HatchBrush" TileMode="Tile"
              Viewport="0,0,5,5" ViewportUnits="Absolute"
              Viewbox="0,0,5,5" ViewboxUnits="Absolute">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <GeometryDrawing Brush="DarkCyan">
                <GeometryDrawing.Geometry>
                    <RectangleGeometry Rect="0,0,5,5"/>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
            <GeometryDrawing Geometry="M0,5 L5,0 M-2,2 L2,-2 M3,7 L7,3">
                <GeometryDrawing.Pen>
                    <Pen Brush="#80ffffff" Thickness="1"/>
                </GeometryDrawing.Pen>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>