c# - 用户控件在设计时作为容器

标签 c# winforms user-controls

我正在设计一个简单的扩展器控件。

我从 UserControl 派生,绘制内部控件,构建,运行;一切都好。

由于内部控件是面板,我想在设计时将其用作容器。事实上,我已经使用了这些属性:

[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))] 

我说的太好了。但它不是...

结果是我可以在设计时将它用作容器但是:

  • 添加的控件返回已经嵌入到用户控件中的内部控件
  • 即使我将在设计时添加的控件推到顶部,在运行时它会再次回到嵌入到用户控件的控件上
  • 我无法在设计时将容器区域限制为面板区域

我错过了什么?这是完整性代码...为什么这段代码不起作用?

[Designer(typeof(ExpanderControlDesigner))]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))] 
public partial class ExpanderControl : UserControl
{
    public ExpanderControl()
    {
        InitializeComponent();
....

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
internal class ExpanderControlDesigner : ControlDesigner
{
    private ExpanderControl MyControl;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        MyControl = (ExpanderControl)component;

        // Hook up events
        ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
        IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));

        s.SelectionChanged += new EventHandler(OnSelectionChanged);
        c.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
    }

    private void OnSelectionChanged(object sender, System.EventArgs e)
    {

    }

    private void OnComponentRemoving(object sender, ComponentEventArgs e)
    {

    }

    protected override void Dispose(bool disposing)
    {
        ISelectionService s = (ISelectionService)GetService(typeof(ISelectionService));
        IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));

        // Unhook events
        s.SelectionChanged -= new EventHandler(OnSelectionChanged);
        c.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);

        base.Dispose(disposing);
    }

    public override System.ComponentModel.Design.DesignerVerbCollection Verbs
    {
        get
        {
            DesignerVerbCollection v = new DesignerVerbCollection();

            v.Add(new DesignerVerb("&asd", new EventHandler(null)));

            return v;
        }
    }
}

我找到了很多资源( Interactiondesignedlimited area ),但没有任何资源可用于操作...

实际上有一个技巧,因为可以设计 System.Windows.Forms 类(像往常一样)并在运行时具有正确的行为(例如 TabControl)。

最佳答案

ParentControlDesigner 不知道您要做什么。它只知道您希望 UserControl 成为容器。

您需要做的是实现您自己的设计器,在面板上启用设计模式:

    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    namespace MyCtrlLib
    {
        // specify my custom designer
        [Designer(typeof(MyCtrlLib.UserControlDesigner))]
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }

            // define a property called "DropZone"
            [Category("Appearance")]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
            public Panel DropZone
            {
                get { return panel1; }
            }
        }

        // my designer
        public class UserControlDesigner  : ParentControlDesigner
        {
            public override void Initialize(System.ComponentModel.IComponent component)
            {
                base.Initialize(component);

                if (this.Control is UserControl1)
                {
                    this.EnableDesignMode(
                       (UserControl1)this.Control).DropZone, "DropZone");
                }
            }
        }
    }

这是我从 Henry Minute 学到的在 CodeProject 上。有关该技术的一些改进,请参阅链接。

关于c# - 用户控件在设计时作为容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2694889/

相关文章:

.net - 如何阻止 winform 设计器将自定义控件的版本放入 .resx 文件中?

c# - 如何将用户控件属性绑定(bind)到其他属性?

c# - 在 WPF 中维护 MySQL 连接的最佳方法是什么?

c# - 如何防范 NHibernate 不完整映射

c# - 如何将 WinForm 用户控件添加到 WPF 中以便我可以在 xaml.cs 文件中引用它

c# - 如何将值传递给winforms c#中的用户控件

c# - Winforms应用程序如何回调上一个窗体?

c# - 如何使自定义提取规则属性数据驱动?

c# - 基于 PIVOT 查询的 Oracle View 不是空值

c# - 在 C# 中打开 Web 浏览器并捕获输出