Повторите кисть фона в WPF - PullRequest
7 голосов
/ 16 августа 2011

и спасибо.

Этот вопрос очень похож на этот старый вопрос без ответа здесь: Как рисовать линии, похожие на блокнот, как фон TextBox? Однако это не то же самое - не совсем.

Я хотел бы создать блокнот на бумажном фоне, но я не знаю, как повторить кисть в XAML. Как поживаете?

EDIT

Вот решение как часть 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>

Ответы [ 3 ]

9 голосов
/ 17 августа 2011
<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>
1 голос
/ 16 августа 2011

Забавно, просто делал то же самое.Вот и я.Вам, вероятно, придется поиграть с TileMode, чтобы установить направление мозаичного изображения, и ViewPort, последние два числа должны быть шириной / высотой вашего изображения (я должен был сделать это, потому что мое изображение растягивалось или просто не появлялосьпрямо).

<ImageBrush x:Key="WindowBackground" ImageSource="/Images/Background.png" TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,4,4" />
0 голосов
/ 16 августа 2011

Используйте ImageBrush

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