从 AdornerLayout 或 Adorner 或装饰控件访问 AdornerPanel?

发布于 2024-12-25 19:40:58 字数 861 浏览 2 评论 0原文

我正在尝试添加一个简单的文本块作为控件的装饰。但我希望它位于我装饰的控件上方。

这是装饰创建(问题不依赖于这段代码):

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

我承认我不清楚 AdornerPanel 和 AdornerLayout 之间的 WPF 类层次结构。

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‖放下 2025-01-01 19:40:58
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);
}
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);
}
七颜 2025-01-01 19:40:58

为了移动你的装饰器,你必须重写 ArrangeOverride 方法并在那里调整新的装饰器位置。

下面是一个带有简单 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
  };

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
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文