Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw dashed line in a WPF adorner

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...

like image 922
flq Avatar asked Jul 01 '10 10:07

flq


2 Answers

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);
like image 157
Samuel Jack Avatar answered Sep 28 '22 03:09

Samuel Jack


You are not stuck to primitives. If you follow this pattern, you can add anything to an adorner.

public class ContainerAdorner : Adorner
{
    // To store and manage the adorner's visual children.
    VisualCollection visualChildren;

    // Override the VisualChildrenCount and GetVisualChild properties to interface with 
    // the adorner's visual collection.
    protected override int VisualChildrenCount { get { return visualChildren.Count; } }
    protected override Visual GetVisualChild(int index) { return visualChildren[index]; }

    // Initialize the ResizingAdorner.
    public ContainerAdorner (UIElement adornedElement)
        : base(adornedElement)
    {
        visualChildren = new VisualCollection(this);
        visualChildren.Add(_Container);
    }
    ContainerClass _Container= new ContainerClass();

    protected override Size ArrangeOverride(Size finalSize)
    {
        // desiredWidth and desiredHeight are the width and height of the element that's being adorned.  
        // These will be used to place the Adorner at the corners of the adorned element.  
        double desiredWidth = AdornedElement.DesiredSize.Width;
        double desiredHeight = AdornedElement.DesiredSize.Height;

        FrameworkElement fe;
        if ((fe = AdornedElement as FrameworkElement) != null)
        {
            desiredWidth = fe.ActualWidth;
            desiredHeight = fe.ActualHeight;
        }

        _Container.Arrange(new Rect(0, 0, desiredWidth, desiredHeight));

        return finalSize;
    }
}
like image 21
Lee Louviere Avatar answered Sep 28 '22 03:09

Lee Louviere