Search code examples
c#wpfadorneradornerlayer

Can you attach an adorner's position to anywhere other than the upper-left of the adorned element?


I can't find any way of fixing an adorner to another point of the adorned element, other than the upper-left point. I know you can move the adorner relative to this point, by implementing ArrangeOverride, but the co-ord system is always based on the top-left.

Any ideas?


Solution

  • Not that I know of. But there is one more possibility to position the Adorner content than ArrangeOverride: AdornerPanel Class. With it's AdornerPlacementCollection Methods

    • PositionRelativeToAdornerHeight Method
    • PositionRelativeToAdornerWidth Method
    • PositionRelativeToContentHeight Method
    • PositionRelativeToContentWidth Method
    • SizeRelativeToAdornerDesiredHeight Method
    • SizeRelativeToAdornerDesiredWidth Method
    • SizeRelativeToContentHeight Method
    • SizeRelativeToContentWidth Method

    you can determine the position and size of your Adorner relative to adorner or content e.g. with a factor and an offset. See this sample to place the Adorner above the adorned control:

    // create AdornerPanel and add your adorner content
    AdornerPanel adornerPanel = new AdornerPanel();
    adornerPanel.Children.Add(yourAdornerContent);
    
    // set placements on AdornerPanel
    AdornerPlacementCollection placement = new AdornerPlacementCollection();
    placement.PositionRelativeToAdornerHeight(-1, 0);
    placement.PositionRelativeToAdornerWidth(1, 0);
    AdornerPanel.SetPlacements(adornerPanel, placement);
    
    // create Adorner with AdornerPanel inside
    Adorner adorner = new YourAdorner(adornedElement)
    {
        Child = adornerPanel
    };