【问题标题】:Repeat a background brush in WPF在 WPF 中重复背景画笔
【发布时间】:2011-08-16 18:33:46
【问题描述】:

谢谢。

此问题与此处未回答的旧问题非常相似: How to paint notebook-like lines as TextBox background? 但是,它不一样 - 不完全一样。

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

编辑

这是作为 TextBox 一部分的解决方案:

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

【问题讨论】:

  • 关于建议的编辑:我同意,请将您的具体解决方案提取到单独的答案中。 (如果需要,还可以添加关于 LineHeightLineStackingStrategy 如何解决对齐问题的注释)

标签: wpf background repeat brush


【解决方案1】:
<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>

【讨论】:

  • 你会调整什么来影响线条的粗细? &lt;Pen Thickness=".5" /&gt; 显然不正确。谢谢。
  • @JerryNixon:实际上应该是Pen.Thickness,如果线条对你来说没有变细,可能会出现混叠问题。
【解决方案2】:

很有趣,只是在做同样的事情。给你。您可能必须使用 TileMode 来设置平铺的方向,以及 ViewPort,最后两个数字应该是图像的宽度/高度(我必须这样做,因为我的图像被拉伸或只是没有出现正确)。

<ImageBrush x:Key="WindowBackground" ImageSource="/Images/Background.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,4,4" />

【讨论】:

  • 您还可以更改ImageBrushStretch 属性以使其正确显示。
【解决方案3】:

使用 ImageBrush

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

【讨论】:

  • 这是我得到的,但它只显示一次图像,居中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 2012-12-18
相关资源
最近更新 更多