Jerry Nixon @Work: Creating a WPF Notepad-like TextBox

Jerry Nixon on Windows

Monday, September 12, 2011

Creating a WPF Notepad-like TextBox

I had a small requirement to create a TextBox in WPF that looked more like a classic Notepad (with muted lines) than the traditional TextBox. Here’s what I came up with:

<Page
 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/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="1">
          <DrawingBrush.Drawing>
              <GeometryDrawing>
                  <GeometryDrawing.Pen>
                      <Pen
Brush="RoyalBlue" Thickness=".1" />
                  </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>
</Page>

Looks like then when we are done:image