Windows窗体设计器具有专用于大多数控件的设计器类。ListView的设计器是System.Windows.Forms.Design.ListViewDesigner,它是System.Design.dll程序集中的内部类。此类使您能够拖动列标题。
UserControl使用System.Windows.Forms.Design.ControlDesigner设计器类。它没有做任何特殊的事情,只是在控件周围使用拖动手柄放置一个矩形。您可以看到标题:将用户控件放在窗体上后,就是用来设计类的是ControlDesigner,列表视图设计器不在图中。因此,您将无法拖动列标题。还要注意,ControlDesigner不允许访问UC内部的控件。
但是,通过创建自己的设计师可以解决此问题。从项目+添加引用开始,选择System.Design。您需要向UC添加公共属性以显示列表视图,并应用[DesignerSerializationVisibility]属性以允许保存更改的属性。并将[Designer]属性应用于UC类以替换默认设计器。一切都类似于此(使用默认名称和显示“员工”的ListView):
using System;using System.ComponentModel;using System.Drawing;using System.Windows.Forms;using System.Windows.Forms.Design; // Note: add reference required: System.Design.dllnamespace 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"); } }}现在可以单击并按常规设计用户控件中的列表视图。



