2011-08-16 22 views
7

e grazie.Ripeti uno sfondo pennello in WPF

Questa domanda è molto simile a questa vecchia domanda senza risposta qui: How to paint notebook-like lines as TextBox background? Tuttavia, non è la stessa - non esattamente.

Mi piacerebbe creare un blocco note, foderato come sfondo di carta, ma non ho familiarità con come ripetere un pennello in XAML. Come?

EDIT

Ecco la soluzione come parte di una 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> 
+0

Per quanto riguarda la modifica suggerita: Sono d'accordo, si prega di estrarre la vostra soluzione concreta in una risposta separata. (Eventualmente aggiungere anche una nota su come 'LineHeight' e' LineStackingStrategy' risolvono il problema di allineamento, se necessario) –

risposta

9
<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> 
+0

Cosa regolereste per influenzare lo spessore della linea? '' non è chiaramente corretto. Grazie. –

+0

@JerryNixon: In realtà dovrebbe essere il 'Pen.Thickness', probabilmente alcuni problemi con l'aliasing se la linea non diventa più sottile per te. –

0

Utilizzare un ImageBrush

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

Questo è quello che ho, ma mostra solo l'immagine una volta, centrata. –

1

divertente, stava solo facendo la stessa cosa. Ecco, vai. Probabilmente dovrai giocare con TileMode per impostare la direzione della piastrellatura, e il ViewPort, gli ultimi due numeri dovrebbero essere la larghezza/altezza dell'immagine (dovevo farlo perché la mia immagine era allungata o semplicemente non veniva fuori a destra).

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