从AdornerLayout或Adorner或被添加装饰的控件中访问AdornerPanel?

3

我希望将一个简单的Textblock作为装饰添加到控件中。但我希望它位于我的装饰控件正上方。

这是装饰创建的代码(问题不在此代码中):

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

我无法实现的是将装饰物定位在装饰控件之上。我想使用这个MSDN代码示例,它使用AdornerPanel来进行定位...
然而,我还没有弄清楚如何访问AdornerPanel对象以应用此MSDN代码示例...无论是从我的装饰控件、AdornedLayout还是Adorner中都不行。
我承认我不太清楚WPF类层次结构中AdornerPanel和AdornerLayout之间的关系。
谢谢您的帮助。
2个回答

3
为了移动您的Adorner,您需要重写ArrangeOverride方法并在其中调整新的adorner位置。
以下是一个使用简单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);
    }

使用方法:

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

  var adorner = new FrameworkElementAdorner(this)
  {
    Child = textBlockMarkTooltipContent
  };

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接