繁体   English   中英

确定自定义Winforms控件的设计时上下文

[英]Determine design-time context of a custom winforms control

假设我有一个自定义WinForms控件:

public class MyBaseControl : Control
{
     ...
}

扩展如下:

public class MyControl : MyBaseControl
{
     ...
}

通过检查this.DesignMode属性标志,可以很this.DesignMode地确定控件是否在视觉上进行设计,但是,有没有一种方法可以确定MyControl本身是否在设计中,而不是在设计时在from上进行操作?


为了提供更多的说明,在MyControl类中,我试图在设计组件本身时区分设计时:

“控制”设计师

在设计时将组件从工具箱添加到表单中时:

在此处输入图片说明

您可以检查控件是否位于设计器的根目录中。
您可以获取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.

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