简体   繁体   English

如何将Button添加为DrawingVisual的子级

[英]How to add a Button as a child of a DrawingVisual

I have a DerivedCanvas class which inherits from Canvas. 我有一个DerivedCanvas类,它继承自Canvas。 I use it to draw several custom drawing visuals, but can't figure out how to add a Button object as a child of a DrawingVisual object. 我用它来绘制几个自定义的绘图视觉效果,但无法弄清楚如何将Button对象添加为DrawingVisual对象的子级。 Below is the code I am using within the DrawingVisual, but nothing shows up at runtime. 以下是我在DrawingVisual中使用的代码,但运行时未显示任何内容。 Is it not possible to add a Button as a child of a DrawingVisual, or am I just doing it wrong? 是否可以将Button添加为DrawingVisual的子级,还是我做错了?

Button button = new Button { Content = "Exit", Width = 70, Height = 23 };
this.Children.Add(button);

Try this (assuming that dv is your DrawingVisual object and size is the size of dv ): 尝试以下操作(假设dv是您的DrawingVisual对象,而sizedv的大小):

foreach (var child in dv.Children)
{
    var element = child as UIElement;
    if (element == null) continue;
    element.Arrange(new Rect(size));
}

1.Add try-catch to handle exception. 1.添加try-catch来处理异常。 2.I guess button has created and added.But coordinate is out of visual range.You should alter coordinate of the button and refresh. 2.我猜按钮已经创建并添加了,但是坐标超出了可视范围。您应该更改按钮的坐标并刷新。

are you sure that your code is executed? 您确定您的代码已执行? Place a breakpoint. 放置一个断点。 Also "this" might nor refer to the object you want.. 同样,“ this”可能也没有指向您想要的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM