如何在自定义控件中启用设计支持? [英] How to enable design support in a custom control?

查看:16
本文介绍了如何在自定义控件中启用设计支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试解释我所追求的.我不知道它的技术术语,所以这里是:

I'll try to explain what I'm after. I don't know the technical term for it, so here goes:

示例 1:如果我在表单上放置一个 ListView 并添加一些列,我可以在设计时单击并拖动这些列来调整它们的大小.

Example 1: If I place a ListView on a Form and add some columns I am able, in Design-Time, to click-and-drag the columns to resize them.

示例 2:现在,我在 UserControl 中放置一个 ListView 并将其命名为MyCustomListView".(也许添加一些方法来增强它).

Example 2: Now, I place a ListView in a UserControl and name it "MyCustomListView" (and perhaps add some method to enhance it somehow).

如果我现在将MyCustomListView"放在在表单上,​​我无法单击并拖动列标题以在设计时调整它们的大小.

If I now place the "MyCustomListView" on a Form I am unable to click-and-drag the column headers to resize them in Design-Time.

有什么方法可以轻松实现吗?某种形式的将单击和拖动事件传递给底层控件,并让该控件发挥作用".我并不是真的想重新编码,只需传递鼠标单击(或其他任何内容)并让 ListView 像上面第一个示例中那样做出反应.

Is there any way to easily make that happen? Some form of "pass the click-and-drag event to the underlying control and let that control do its magic". Im not really looking to recode, just pass on the mouseclick (or whatever it is) and let the, in this case, ListView react as it did in the first example above.

推荐答案

Windows 窗体设计器为大多数控件提供了专用的设计器类.ListView 的设计器是 System.Windows.Forms.Design.ListViewDesigner,它是 System.Design.dll 程序集中的一个内部类.此类使您能够拖动列标题.

The Windows Forms designer has dedicated designer classes for most controls. The designer for a ListView is System.Windows.Forms.Design.ListViewDesigner, an internal class in the System.Design.dll assembly. This class gives you the ability to drag the column headers.

UserControl 使用 System.Windows.Forms.Design.ControlDesigner 设计器类.它没有做任何特别的事情,只是在控件周围放置一个带有拖动手柄的矩形.您可以看到它的标题:在您将用户控件放在表单上之后,用于设计类的是 ControlDesigner,而 ListViewDesigner 不在图中.因此,您将失去拖动列标题的能力.另请注意,ControlDesigner 无法访问 UC 内部的控件.

A UserControl uses the System.Windows.Forms.Design.ControlDesigner designer class. It doesn't do anything special, just puts a rectangle around the control with drag handles. You can see where this is heading: after you put your user control on a form, it is ControlDesigner that is used to design the class, ListViewDesigner is not in the picture. You thus lose the ability to drag the column headers. Also note that ControlDesigner doesn't give access to the controls inside the UC.

但是,通过创建您自己的设计师可以解决这个问题.从 Projects + Add Reference 开始,选择 System.Design.您需要向 UC 添加公共属性以公开列表视图并应用 [DesignerSerializationVisibility] 属性以允许保存更改的属性.并将 [Designer] 属性应用到 UC 类以替换默认设计器.一切都应该像这样(使用默认名称和显示员工"的 ListView):

That's fixable however by creating your own designer. Start with Projects + Add Reference, select System.Design. You'll need to add a public property to the UC to expose the list view and apply the [DesignerSerializationVisibility] attribute to allow changed properties to be saved. And apply the [Designer] attribute to the UC class to replace the default designer. It all should resemble this (using the default names and a ListView that displays "employees"):

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;   // Note: add reference required: System.Design.dll

namespace WindowsFormsApplication1 {
    [Designer(typeof(MyDesigner))]   // Note: custom designer
    public partial class UserControl1 : UserControl {
        public UserControl1() {
            InitializeComponent();
        }

        // Note: property added
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ListView Employees { get { return listView1; } }
    }

    // Note: custom designer class added
    class MyDesigner : ControlDesigner {
        public override void Initialize(IComponent comp) {
            base.Initialize(comp);
            var uc = (UserControl1)comp;
            EnableDesignMode(uc.Employees, "Employees");
        }
    }
}

用户控件中的列表视图现在可以正常单击和设计.

The list view in the user control can now be clicked and designed as normal.

这篇关于如何在自定义控件中启用设计支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆