Anders Gustafsson Anders Gustafsson - 2 months ago 32
C# Question

Can I draw dashed lines in WPF Toolkit chart?

In my WPF based application, I am using the Data Visualization Charting component available in the WPF Toolkit. I want to draw dashed lines, similar to the illustration from this SO answer:

Solid and dashed lines

Unfortunately, this only works with Windows Forms, since the BorderDashStyle property only exists in the Windows Forms version of the DataVisualization.Charting component and not in the WPF Toolkit equivalent.

How should I go about to generate dashed lines with the WPF Toolkit charting component?


I searched for an analogous solution for the Silverlight Toolkit charting component, and found this.

Luckily, it turns out that the same approach can be applied in WPF. By setting the property LineSeries.PolylineStyle to a System.Windows.Shapes.Polyline style with a suitable Shape.StrokeDashArray property setting, the desired line dash can be obtained.

Programmatically, it can be accomplished with something like this:

var series = new LineSeries
         ItemsSource = calcData,
         IndependentValuePath = "X",
         DependentValuePath = "Y",
         PolylineStyle = GetDashedLineStyle()


Style GetDashedLineStyle()
    var style = new Style(typeof(Polyline));
    style.Setters.Add(new Setter(Shape.StrokeDashArrayProperty, 
                      new DoubleCollection(new[] { 5.0 })));
    return style;