0

我有这个例子作为 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;

老实说,我不知道如何编写路径数据。我怎样才能做到这一点?

4

1 回答 1

1

你可以写

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>
于 2018-08-08T10:00:04.053 回答