Use a custom dash pattern with WPF and XAML in C#


My post Render dashed lines in a WPF program using C# explains how a WPF program can use C# code to render lines with a custom dash pattern onto a bitmap. This post shows how to use a custom dash pattern min XAML code.

The key is to use a StrokeDashArray property to define a line’s dash pattern. This program uses the following code to draw its curve.

<Path Stroke="Red" StrokeThickness="5"
    Data="M 175,10 C -100,150 400,200 70,30">

The custom dash pattern 10,1,1,1 means the line should draw 10 units, skip 1 unit, draw 1 unit, skip 1 unit, and then repeat.

Note that the units are equal to the width of the line. In this example, the line is 5 pixels wide so each unit is 5 pixels. that means this custom dash pattern makes the line draw 50 pixels, skip 5 pixels, draw 5 pixels, skip 5 pixels, and repeat.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, geometry, graphics, wpf, XAML and tagged , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.