0070 0070 - 25 days ago 20
C# Question

How to set the WPF StrokeDashArray from Code behind?

Can i know how to set the StrokeDashArray from code behind? I try to use the method like the way how to set the margin...but it doesn't work.

The following is my code:

public static void DrawCircle(MainWindow main)
{
Ellipse myCircle = new Ellipse();
myCircle.Stroke = Brushes.Orange;
myCircle.Width = 25;
myCircle.Height = 25;
myCircle.StrokeThickness = 2;
myCircle.StrokeDashArray = new System.Windows.Thickness("2,2,2,2");

Canvas.SetLeft(myCircle, 10);
Canvas.SetRight(myCircle, 10);
Canvas.SetBottom(myCircle, 20);
Canvas.SetTop(myCircle, 20);
main.MyCanvas.Children.Add(myCircle);

}

Answer

Problem is this line

myCircle.StrokeDashArray = new System.Windows.Thickness("2,2,2,2");

StrokeDashArray is a DoubleCollection

Try this instead

myCircle.StrokeDashArray = new DoubleCollection() { 2 };
Comments