确定自定义 Winforms 控件的设计时上下文
Determine design-time context of a custom winforms control
假设我有一个自定义 WinForms 控件:
public class MyBaseControl : Control
{
...
}
扩展如下:
public class MyControl : MyBaseControl
{
...
}
通过检查 this.DesignMode
属性 标志,可以很容易地确定控件是否经过视觉设计,但是 有没有办法确定 MyControl
本身是被设计的,而不是在设计时被操纵的?
为了提供额外的说明,在 MyControl
class 中,我试图在设计组件本身时区分设计时:
并且当组件在设计时从工具箱添加到表单时:
您可以在设计器中检查该控件是否为root。
您可以获取 IDesignerHost
service and then check the 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;
}
}
}
假设我有一个自定义 WinForms 控件:
public class MyBaseControl : Control
{
...
}
扩展如下:
public class MyControl : MyBaseControl
{
...
}
通过检查 this.DesignMode
属性 标志,可以很容易地确定控件是否经过视觉设计,但是 有没有办法确定 MyControl
本身是被设计的,而不是在设计时被操纵的?
为了提供额外的说明,在 MyControl
class 中,我试图在设计组件本身时区分设计时:
并且当组件在设计时从工具箱添加到表单时:
您可以在设计器中检查该控件是否为root。
您可以获取 IDesignerHost
service and then check the 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;
}
}
}