Problem solve Get help with specific problems with your technologies, process and projects.

Understanding line stroke patterns

WPF provides a number of useful shapes to use in your UI shapes. Learn how to best utilize line stroke patterns.

WPF provides a number of useful shapes to use in your UI shapes (Rectangle, Ellipse, Line, PolyLine, Polygon, Path). These shape classes have two brush properties: the Fill brush is used to fill the interior of the shape and the Stroke brush is used to paint an outline around the filled interior. If you want the stroke to be visible you must also provide a StrokeThickness as shown in the following XAML.

   <Rectangle  Width='400'
               StrokeThickness='15' />

You can exert further control over how the stroke is drawn by modifying additional properties. Let's start by considering the Stroke Dash properties.

Adding dashes to the stroke
The stroke doesn't have to be drawn as a solid entity between two points. You can split the line into a series of dashes with the StrokeDashArray property. As the name implies, StrokeDashArray holds an array of double values. The first value in the DashArray indicates the length of the solid part of the line. The second value in the array indicates the length of the gap. This pattern can continue, add a third value to the array to designate the next solid section length, a fourth value denotes the next gap length and so on. The length is determined by multiplying the array value by the StrokeThickness. In the following example, the lengths are multiples of 15 (solid 30, gap 15, solid 45, gap 30).


Here is a screen shot showing a (1,1) pattern.

Figure 1 - Pattern (1,1)

Here is a screenshot showing a (5,1) pattern.

Figure 2 - Pattern (5,1)

Adding end caps
By default each solid dash segment is drawn with a flat end cap on each end of the dash. You can change the end caps on the dash by setting the StrokeDashCap property. There are four choices available: Flat, Round, Triangle and Square.

Figure 3 - Triangle Dash Cap

Dig Deeper on Windows Presentation Foundation