通过背后的代码创建VisualBrush [英] Create VisualBrush by code behind

查看:110
本文介绍了通过背后的代码创建VisualBrush的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个示例作为XAML:

I have this example as 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>

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

I need to write the same in code behind but I've been able to do only this:

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

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

Honestly I dunno how to write the Path Data. How can I do this?

推荐答案

您可以编写

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.

However, you don't need to use a VisualBrush at all.

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

The XAML below shows how to use a DrawingBrush with two drawings to get the hatch pattern on top of a solid color background.

<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>

这篇关于通过背后的代码创建VisualBrush的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆