WPF中的Graph Paper使用DrawingBrush

时间:2015-07-16 02:04:51

标签: wpf xaml drawingbrush

我正在尝试使用DrawingBrush使用WPF制作一些Graph Paper。我在MSDN上找到了以下示例,它非常接近我想要的但不完全正确。我想做的就是纯XAML。我是WPF的新手。

 <DrawingBrush x:Key="GridTile" 
                  Viewport="0,0,10,10" 
          ViewportUnits="Absolute"
          TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="Blue" />
                <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="Red" />
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

目前生成

enter image description here

我想生成

宽度为3厘米,每行为4毫米

enter image description here

我将使用这个tile我的背景,或者说DrawingBrush TileMode会为我处理。

1 个答案:

答案 0 :(得分:3)

更改画笔的大小,使视口的高度高于宽度,并相应地更改几何体,使线条仍显示为1px厚。

<Window x:Class="WPFTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF"
        SizeToContent="WidthAndHeight">
    <Window.Resources>
        <DrawingBrush x:Key="GridTile" 
          Viewport="0,0,4,16" 
          ViewportUnits="Absolute"
          TileMode="Tile">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Geometry="M0,0 L1,0 1,0.05, 0,0.05Z" Brush="Black" />
                    <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="Black" />
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Rectangle Height="512" Width="512" Stroke="Black" StrokeThickness="0" Fill="{StaticResource GridTile}"/>
</Window>

enter image description here