Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve a dashed or dotted border in WPF?

I have a ListViewItem that I am applying a Style to and I would like to put a dotted grey line as the bottom Border.

How can I do this in WPF? I can only see solid color brushes.

like image 715
dan Avatar asked Jun 01 '11 01:06

dan


People also ask

How do I add a border in WPF?

To place a border around an element, WPF provides the Border element. Similar to other WPF elements, the Border has Width, Height, Background, and HorizontalAlignment and VerticalAlignment properties. Besides these common properties, Border has two properties that make Border a border.

How do you add a border in XAML?

To apply a border to a XAML element, you can place the element within a Border element, The BorderThickness and BorderBrush are two main properties of a Border The BorderBrush property represents the brush that is used to draw the border. The BorderThickness property represents the thickness of the border.


2 Answers

This worked great in our application, allowing us to use a real Border and not mess around with Rectangles:

<Border BorderThickness="1,0,1,1">    <Border.BorderBrush>       <DrawingBrush Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">          <DrawingBrush.Drawing>             <DrawingGroup>                <GeometryDrawing Brush="Black">                   <GeometryDrawing.Geometry>                      <GeometryGroup>                         <RectangleGeometry Rect="0,0,50,50" />                         <RectangleGeometry Rect="50,50,50,50" />                      </GeometryGroup>                   </GeometryDrawing.Geometry>                </GeometryDrawing>             </DrawingGroup>          </DrawingBrush.Drawing>       </DrawingBrush>    </Border.BorderBrush>     <TextBlock Text="Content Goes Here!" Margin="5"/> </Border> 

Note that the Viewport determines the size of the dashes in the lines. In this case, it generates eight-pixel dashes. Viewport="0,0,4,4" would give you four-pixel dashes.

like image 166
Rand Scullard Avatar answered Sep 20 '22 18:09

Rand Scullard


You can create a dotted or dashes line using a rectangle like in the code below

<Rectangle Stroke="#FF000000" Height="1" StrokeThickness="1" StrokeDashArray="4 4"                                                        SnapsToDevicePixels="True"/> 

Get started with this and customize your listview according to your scenario

like image 45
biju Avatar answered Sep 18 '22 18:09

biju