WPF - 将 ContentControl 添加到自定义画布
我有一个继承自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题出在 DrawingCanvas 中缺少 ArrangeOverride 方法。将以下 ArrangeOverride 方法添加到 DrawingCanvas 后,ContentControls 将正确显示。
其中 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.
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.