【问题标题】:Graph paper with elongated grid sections?带有细长网格部分的方格纸?
【发布时间】:2015-07-16 02:04:51
【问题描述】:

我正在尝试使用 DrawingBrush 使用 WPF 制作一些方格纸。

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

目前这会生成

我要生成

宽3cm,每行4mm

我将使用此图块作为我的背景,或者更确切地说是 DrawingBrush TileMode 为我处理。

【问题讨论】:

  • 该模式创建了一个平铺的网格模式。它看起来被拉伸的原因是因为你的容器。你在什么物体上使用画笔?
  • @Cyral 感谢您的回复。图片来自我收到的一张图片。它不是由 WPF 生成的。我想要做的是创建相同的图像,但使用 wpf。
  • 哦,您提供的代码运行良好,您只需要知道如何处理它吗?编辑:你的意思是你希望垂直线更接近?
  • @Cyral 完全正确......垂直线需要相距 1 毫米,水平线需要相距 4 毫米......这将非常有帮助!!!
  • 使用毫米是另一个话题,见How to specify WPF control size in millimeters using XAML

标签: wpf xaml drawingbrush


【解决方案1】:

更改Brush 的大小,使Viewport 的高度大于宽度,并相应地更改Geometry,使线条仍然显得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>

【讨论】:

  • 嘿@Cyral 你所做的真是太棒了,我正试图用深色边框来接近上面的图像(我更新了它),以创建 12 毫米 x 30 毫米的部分。是否可以帮助我完成这最后一步,或者只是建议我自己去做?
猜你喜欢
  • 1970-01-01
  • 2020-05-28
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多