在WPF中重复背景笔刷

在WPF中重复背景笔刷,wpf,background,repeat,brush,Wpf,Background,Repeat,Brush,谢谢你 这个问题与这里这个古老的、未回答的问题非常相似: 然而,这是不一样的-不完全一样 我想创建一个记事本,像纸一样的背景,但我不熟悉如何在XAML中重复画笔。你好吗 编辑 以下是作为文本框一部分的解决方案: <TextBox TextBlock.LineHeight="20" TextBlock.LineStackingStrategy="BlockLineHeight" Padding="20,10,20,20" TextWrapping="W

谢谢你

这个问题与这里这个古老的、未回答的问题非常相似: 然而,这是不一样的-不完全一样

我想创建一个记事本,像纸一样的背景,但我不熟悉如何在XAML中重复画笔。你好吗

编辑

以下是作为文本框一部分的解决方案:

<TextBox TextBlock.LineHeight="20" 
         TextBlock.LineStackingStrategy="BlockLineHeight" 
         Padding="20,10,20,20" TextWrapping="Wrap">
  <TextBox.Background>
    <DrawingBrush TileMode="Tile" Stretch="None" Viewport="0,0,20,20" 
                  ViewportUnits="Absolute" Opacity=".07">
      <DrawingBrush.Drawing>
          <GeometryDrawing>
              <GeometryDrawing.Pen>
                  <Pen Brush="RoyalBlue" />
              </GeometryDrawing.Pen>
              <GeometryDrawing.Geometry>
                  <LineGeometry StartPoint="0,0" EndPoint="20,0"/>
              </GeometryDrawing.Geometry>
          </GeometryDrawing>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </TextBox.Background>
  Now is the time for all good men to come to the aid of their country.
  Now is the time for all good men to come to the aid of their country.
  Now is the time for all good men to come to the aid of their country.
  Now is the time for all good men to come to the aid of their country.
  Now is the time for all good men to come to the aid of their country.
</TextBox>

现在是所有好人来帮助他们国家的时候了。
现在是所有好人来帮助他们国家的时候了。
现在是所有好人来帮助他们国家的时候了。
现在是所有好人来帮助他们国家的时候了。
现在是所有好人来帮助他们国家的时候了。
使用图像画笔

<ImageBrush ImageSource="image.png" TileMode="Tile"/>

有趣的是,我也在做同样的事情。给你。您可能需要使用TileMode来设置平铺方向和视口,最后两个数字应该是图像的宽度/高度(我必须这样做,因为我的图像被拉伸了,或者没有正确显示)




您会调整什么来影响线条的厚度<代码>显然不正确。谢谢。@JerryNixon:实际上应该是
笔的厚度
,如果线条没有变细,可能会出现一些别名问题。关于建议的编辑:我同意,请将您的具体解决方案提取到单独的答案中。(如果需要,还可以添加一条关于如何
LineHeight
LineStackingStrategy
解决对齐问题的注释)这是我得到的,但它只显示一次居中的图像。您还可以更改
ImageBrush
拉伸属性,使其正确显示。
<ImageBrush x:Key="WindowBackground" ImageSource="/Images/Background.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,4,4" />
<DrawingBrush TileMode="Tile" Stretch="None"
              Viewport="0,0,20,20" ViewportUnits="Absolute">
    <DrawingBrush.Drawing>
        <GeometryDrawing>
            <GeometryDrawing.Pen>
                <Pen Brush="Gray"/>
            </GeometryDrawing.Pen>
            <GeometryDrawing.Geometry>
                <LineGeometry StartPoint="0,0"
                              EndPoint="20,0"/>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>