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

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

问题描述

我会尽力解释我后。我不知道它的技术术语,所以这里有云:

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:
现在,我把一个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 know place the "MyCustomListView" on a Form I am unable to click-and-drag the column headers to resize them in Design-Time.

有没有什么办法可以轻松地做到这一点?的某种形式的通过点击和拖动事件的基本控制,并让该控件做它的神奇。我不是真的希望重新code,刚通上点击鼠标(或不管它是什么),并让,在这种情况下,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.

一个用户控件使用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.

这是通过创建自己的设计师是可以解决的不过。开始与项目+添加引用,选择System.Design。你需要一个公共属性添加到UC揭露列表视图和应用[DesignerSerializationVisibility]属性以允许更改的属性进行保存。而[设计]属性应用到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天全站免登陆