DrawingContext 装饰器 - 可以绘制堆栈面板吗?

发布于 2024-12-22 14:41:03 字数 265 浏览 2 评论 0原文

通过使用 DrawingContext 类,我能够使用 DrawRectangle() 方法成功地将装饰器添加到我的 adornedElement 中。

是否可以构建一个堆栈面板,其中包含文本框和图像 - 并将其指定为装饰器?

顺便说一句,我使用的是 Visual Studio 2010 - 不是 microsoft expression。

非常感谢,

Dan

Using the DrawingContext class I've been able to use the DrawRectangle() method to successfully add an adorner to my adornedElement.

Is it possible to construct a stackpanel, with a textbox and image inside - and assign this as the adorner?

I'm using visual studio 2010 by the way - not microsoft expression.

Thanks a lot,

Dan

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

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

发布评论

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

评论(1

极度宠爱 2024-12-29 14:41:03

不,这不可能开箱即用,因为 DrawingContext 仅用于绘制视觉对象,而不用于绘制 FrameworkElements。
您可以做的是创建自己的 Adorner,它能够绘制 FrameworkElements:

  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)
    {
      _child.Arrange(new Rect(new Point(0, 0), finalSize));
      return new Size(_child.ActualWidth, _child.ActualHeight);
    }
  }

用法:

  var fweAdorner = new FrameworkElementAdorner(adornedElement);

  //Create your own Content, here: a StackPanel with some stuff inside
  var stackPanel = new StackPanel();
  stackPanel.Children.Add(new TextBox() { Text="TEST"});
  stackPanel.Children.Add(new Image());

  fweAdorner.Child = stackPanel;

  var adornerLayer = AdornerLayer.GetAdornerLayer(adornedElement);
  adornerLayer.Add(fweAdorner);

如果多次使用 StackPanel 的这种组合,您还可以将 StackPanel 的创建直接合并到 Adorner 中。这取决于你的场景。

No, this is not possible out of the box as the DrawingContext is only meant to draw Visuals and no FrameworkElements.
What you can do is create your own Adorner which is able to draw FrameworkElements:

  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)
    {
      _child.Arrange(new Rect(new Point(0, 0), finalSize));
      return new Size(_child.ActualWidth, _child.ActualHeight);
    }
  }

Usage:

  var fweAdorner = new FrameworkElementAdorner(adornedElement);

  //Create your own Content, here: a StackPanel with some stuff inside
  var stackPanel = new StackPanel();
  stackPanel.Children.Add(new TextBox() { Text="TEST"});
  stackPanel.Children.Add(new Image());

  fweAdorner.Child = stackPanel;

  var adornerLayer = AdornerLayer.GetAdornerLayer(adornedElement);
  adornerLayer.Add(fweAdorner);

You could also incorporate the creation of the StackPanel directly in the Adorner if you use this combination of a StackPanel multiple times. That depends on your scenario.

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