Search code examples
c#wpfadorner

Accessing AdornerPanel from an AdornerLayout or an Adorner or a adorned Control?


I am trying to add an simple Textblock as adorment to a control. But I want it to be positionned just above my adorned control.

This is the decoration creation ( the problem doesnt rely in this code):

public void AddLabelDecoration()
{
    AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);

    TextBlock textBlockMarkTooltipContent = new TextBlock();
    textBlockMarkTooltipContent.Text = "Test Label Adorner";

    _labelAdornerMarkTooltipContentAdorner = new Adorner(this)
    {
        Child = textBlockMarkTooltipContent 
    };

    adornerLayer.Add(_labelAdornerMarkTooltipContentAdorner);
}

What I cannot achieve to do, is the positionning of the Decoration, above the adorned control. I would like to use this MSDN code sample, which makes use of AdornerPanel so as to do the positionning...

However I have not figured out how to access to an AdornerPanel object so as to apply this MSDN code sample... neither from my adorned control, from the AdornedLayout, or the Adorner...

I admit I don't clear understand the WPF class hierarchy between AdornerPanel and AdornerLayout.

Any help appreciated.


Solution

  • public void AddLabelDecoration()
    {
        AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
    
        TextBlock textBlockMarkTooltipContent = new TextBlock();
        textBlockMarkTooltipContent.Text = "Test Label Adorner";
    
        AdornerPanel labelAdornerAdornerPanel = new AdornerPanel();
    
        // add your TextBlock to AdornerPanel
        labelAdornerAdornerPanel.Children.Add(textBlockMarkTooltipContent);
    
        // set placements on AdornerPanel
        AdornerPlacementCollection placement = new AdornerPlacementCollection();
        placement.PositionRelativeToAdornerHeight(-1, 0);
        placement.PositionRelativeToAdornerWidth(1, 0);
        AdornerPanel.SetPlacements(labelAdornerAdornerPanel, placement);
    
        // create Adorner with AdornerPanel inside
        _labelAdornerMarkTooltipContentAdorner = new Adorner(this)
        {
            Child = labelAdornerAdornerPanel
        };
    
        adornerLayer.Add(_labelAdornerMarkTooltipContentAdorner);
    }