[英]Determine design-time context of a custom winforms control
Suppose I have a custom WinForms control: 假设我有一个自定义WinForms控件:
public class MyBaseControl : Control
{
...
}
which is extended like: 扩展如下:
public class MyControl : MyBaseControl
{
...
}
By checking the this.DesignMode
property flag it is fairly trivial to determine whether or not the control is being visually designed, however is there a way to determine if the MyControl
itself is being designed, versus being manipulated on a from at design time? 通过检查
this.DesignMode
属性标志,可以很this.DesignMode
地确定控件是否在视觉上进行设计,但是,有没有一种方法可以确定MyControl
本身是否在设计中,而不是在设计时在from上进行操作?
To provide additional clarification, within the MyControl
class, I am trying to differentiate between design-time when the component itself is being designed: 为了提供更多的说明,在
MyControl
类中,我试图在设计组件本身时区分设计时:
and when the component is added to a form from the toolbox at design-time: 在设计时将组件从工具箱添加到表单中时:
You can check if the control is root in designer or not. 您可以检查控件是否位于设计器的根目录中。
You can get the IDesignerHost
service and then check the RootComponent
property to see if your control is the root component for current designer or not. 您可以获取
IDesignerHost
服务,然后检查RootComponent
属性,以查看您的控件是否是当前设计器的根组件。
using System.Windows.Forms;
using System.ComponentModel.Design;
public partial class MyBaseControl : UserControl
{
public MyBaseControl()
{
InitializeComponent();
}
public bool IsRootDesigner
{
get
{
var host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
if (host != null)
return host.RootComponent == this;
return false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.