tags:

views:

43

answers:

1

Hi there,

I have found several articles on the Web regarding drawing a dashed line in WPF. However, they seem to revolve around using the Line-class, which is a UIElement in WPF. It goes something like this:

Line myLine = new Line();
DoubleCollection dashes = new DoubleCollection();
dashes.Add(2);
dashes.Add(2);
myLine.StrokeDashArray = dashes;

Now, I am inside an Adorner, where I only have access to a Drawing Context. There, I am more or less reduced to the Drawing primitives, Brushes, Pens, geometry etc. This looks more like that:

var pen = new Pen(new SolidColorBrush(Color.FromRgb(200, 10, 20)), 2);
drawingContext.DrawLine(pen, point1, point2);

I am stuck how to do a dashed line on this level of the API. I hope it isn't down to "draw the small lines one by one" but rather something else I haven't seen...

+2  A: 

Look at the Pen.DashStyle property. You can either use members of the DashStyles class which give some predefined dash styles, or you can specify you own pattern of dashes and gaps by creating a new DashStyle instance.

var pen = new Pen(new SolidColorBrush(Color.FromRgb(200, 10, 20)), 2);
pen.DashStyle = DashStyles.Dash;
drawingContext.DrawLine(pen, point1, point2);
Samuel Jack
Doh, that's it, somehow I missed that property. It's 35+°C in Germany right now :)
flq