WPF - 将 ContentControl 添加到自定义画布

发布于 2024-10-11 01:56:32 字数 1040 浏览 7 评论 0原文

我有一个继承自 Canvas 的自定义 DrawingCanvas。当我使用以下代码将 ContentControl 添加到 DrawingCanvas 时,没有任何显示。

GraphicsRectangle rect = new GraphicsRectangle(0, 0, 200, 200, 5, Colors.Blue);
DrawingContainer host = new DrawingContainer(rect);

ContentControl control = new ContentControl();
control.Width = 200;
control.Height = 200;
DrawingCanvas.SetLeft(control, 100);
DrawingCanvas.SetTop(control, 100);
control.Style = Application.Current.Resources["DesignerItemStyle"] as Style;

control.Content = host;

drawingCanvas.Children.Add(control);

GraphicsRectangle 是一个 DrawingVisual,上面的构造函数将一个左上角为 (0,0) 点、长度为 200 的 Rect 绘制到 GraphicsRectangle 的绘图上下文中。 DrawingContainer 是一种 FrameworkElement,它有一个子元素,即上面的矩形,由构造函数给出。 DrawingContainer 实现 GetVisualChild 和 VisualChildrenCount 重写方法。最后,将 ContentControl 的 Content 属性设置为 DrawingContainer,以便能够显示 DrawingVisual 的内容。

当我将创建的 ContentControl 添加到常规 Canvas 时,控件显示正确。我猜原因是 DrawingCanvas 没有实现 ArrangeOverride 方法。它仅实现MeasureOverride方法。另外 DrawingContainer 不实现 Measure 和 安排覆盖方法。有什么想法吗?

I have a custom DrawingCanvas which is inherited from Canvas. When I add a ContentControl to DrawingCanvas with the following code nothing shows up.

GraphicsRectangle rect = new GraphicsRectangle(0, 0, 200, 200, 5, Colors.Blue);
DrawingContainer host = new DrawingContainer(rect);

ContentControl control = new ContentControl();
control.Width = 200;
control.Height = 200;
DrawingCanvas.SetLeft(control, 100);
DrawingCanvas.SetTop(control, 100);
control.Style = Application.Current.Resources["DesignerItemStyle"] as Style;

control.Content = host;

drawingCanvas.Children.Add(control);

GraphicsRectangle is a DrawingVisual and the constructor above draws a Rect with (0,0) top left point and length of 200 to the drawingContext of GraphicsRectangle. DrawingContainer is a FrameworkElement and it has one child, which is rect above, given with constructor. DrawingContainer implements GetVisualChild and VisualChildrenCount override methods. At last, Content property of ContentControl is set to the DrawingContainer to be able to show the DrawingVisual's content.

When I add the created ContentControl to a regular Canvas, control is showed correctly. I guess the reason is that DrawingCanvas doesn't implement ArrangeOverride method. It only implements MeasureOverride method. Also DrawingContainer doesn't implement Measure and
Arrange override methods. Any ideas?

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

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

发布评论

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

评论(1

妳是的陽光 2024-10-18 01:56:32

我认为问题出在 DrawingCanvas 中缺少 ArrangeOverride 方法。将以下 ArrangeOverride 方法添加到 DrawingCanvas 后,ContentControls 将正确显示。

protected override Size ArrangeOverride(Size arrangeSize)
{
    foreach (Visual child1 in children)
    {
        if (child1 is DrawingVisual)
            continue;

        ContentControl child = child1 as ContentControl;
        GraphicsBase content = ((DrawingContainer)(child.Content)).GraphicsObject;

        child.Arrange(new Rect(DrawingCanvas.GetLeft(child), DrawingCanvas.GetTop(child), content.Width, content.Height));
     }

     return arrangeSize;
}

其中 GraphicsBase 是 GraphicsRectangle 类的基类。为了找到 GraphicsBase 的大小,我向 GraphicsBase 添加了 width 和 height 属性,这些属性在 GraphicsRectangle 的构造函数中设置。

As I thought the problem was missing ArrangeOverride method in DrawingCanvas. With the following ArrangeOverride method added to DrawingCanvas, ContentControls are showed correctly.

protected override Size ArrangeOverride(Size arrangeSize)
{
    foreach (Visual child1 in children)
    {
        if (child1 is DrawingVisual)
            continue;

        ContentControl child = child1 as ContentControl;
        GraphicsBase content = ((DrawingContainer)(child.Content)).GraphicsObject;

        child.Arrange(new Rect(DrawingCanvas.GetLeft(child), DrawingCanvas.GetTop(child), content.Width, content.Height));
     }

     return arrangeSize;
}

where GraphicsBase is the base of the GraphicsRectangle class. In order to find the size of the GraphicsBase, I added width and height properties to GraphicsBase which are set in the constructor of GraphicsRectangle.

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