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

StackOverflow https://stackoverflow.com/questions/8787980

  •  15-04-2021
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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);
}

نصائح أخرى

In order to move your Adorner you have to override the ArrangeOverride method and adjust a new adorner position there.

Here's an example with a simple FrameworkElementAdorner.

  public class FrameworkElementAdorner : Adorner
  {
    private FrameworkElement _child;

    public FrameworkElementAdorner(UIElement adornedElement)
      : base(adornedElement)
    {
    }

    protected override int VisualChildrenCount
    {
      get { return 1; }
    }

    public FrameworkElement Child
    {
      get { return _child; }
      set
      {
        if (_child != null)
        {
          RemoveVisualChild(_child);
        }
        _child = value;
        if (_child != null)
        {
          AddVisualChild(_child);
        }
      }
    }

    protected override Visual GetVisualChild(int index)
    {
      if (index != 0) throw new ArgumentOutOfRangeException();
      return _child;
    }

    protected override Size MeasureOverride(Size constraint)
    {
      _child.Measure(constraint);
      return _child.DesiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      // Adjust your offset here:
      _child.Arrange(new Rect(new Point(-20, -20), finalSize));
      return new Size(_child.ActualWidth, _child.ActualHeight);
    }

Usage:

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

  var adorner = new FrameworkElementAdorner(this)
  {
    Child = textBlockMarkTooltipContent
  };
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top