简体   繁体   English

控件的自定义设计器

[英]Custom Designer for a Control

I've got a custom class which derives from SplitContainer: 我有一个派生自SplitContainer的自定义类:

namespace Builder.Components
{
    public partial class ProjectSidebar : SplitContainer
    {
        public ProjectSidebar()
        {
            InitializeComponent();
        }
    }
}

Now when I right-click and select View Designer I'd like to see the SplitContainer and edit it, like I would with the default controls (drop a panel in it, etc.). 现在,当我右键单击并选择View Designer时,我希望看到SplitContainer并对其进行编辑,就像我使用默认控件一样(在其中放置一个面板等)。 All I see is a message to add controls and switch to Code view . 我看到的只是添加控件和切换到代码视图的消息

How to achieve this? 怎么做到这一点?

In order to add design-time functionality like additional operations (known as Action Lists and Verbs depending on how they are provided), or dragging of visual elements like headers or split bars, you need to implement a custom designer (usually derived from ControlDesigner ) that allows the Windows Forms designer to understand how to interact with your custom control at design-time. 为了添加设计时功能,如附加操作(根据提供的方式称为动作列表和动词),或拖动视觉元素(如标题或拆分条),您需要实现自定义设计器(通常从ControlDesigner派生)这允许Windows窗体设计器了解如何在设计时与自定义控件进行交互。

MSDN has a lengthy section on adding design-time support for your controls. MSDN有一个冗长的部分 ,为您的控件添加设计时支持。 It describes everything from type converters to extender providers and designer serialization to designer customization . 它描述了从类型转换器扩展器提供程序设计器序列化设计器定制的所有内容

Additional resources 其他资源

This article on CodeProject contains information on creating designers for custom controls. CodeProject上的这篇文章包含有关为自定义控件创建设计器的信息。 There are also some useful tips here and in this other StackOverflow question . 也有一些有用的技巧在这里和在这个其他的StackOverflow问题

Only the Form and the UserControl classes have designers that allow you to edit their child controls at design time. 只有Form和UserControl类具有允许您在设计时编辑其子控件的设计器。 Creating your own designer to give SplitContainer the same behavior is not exactly simple, most of all because it is so poorly documented and difficult to debug. 创建自己的设计器以使SplitContainer具有相同的行为并不是非常简单,最重要的是因为它的文档记录很差且难以调试。 You'll need to study the framework code with Reflector to get it right. 您需要使用Reflector学习框架代码才能使其正确。

Punt this problem, put the SplitContainer in a user control. 解决这个问题,将SplitContainer放在用户控件中。 Set its Dock property to Fill. 将其Dock属性设置为Fill。 Now it is easy. 现在很容易。

When writing your control, you need to make sure to add design time logic to it. 编写控件时,需要确保为其添加设计时逻辑。

See this (.NET 1.0) article on MSDN for some more details. 有关更多详细信息,请参阅MSDN上的此(.NET 1.0)文章 Here is another article from 2003. 是2003年的另一篇文章。

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

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