我会试着解释一下我的追求.我不知道它的技术术语,所以这里是:
示例1:如果我在表单上放置ListView并添加一些列,我可以在"设计时"中单击并拖动列以调整它们的大小.
示例2:现在,我将ListView放在UserControl中并将其命名为"MyCustomListView"(并且可能添加一些方法来以某种方式增强它).
如果我知道在表单上放置"MyCustomListView",我无法单击并拖动列标题以在"设计时"中调整它们的大小.
有没有办法轻松实现这一目标?某种形式的"将点击并拖动事件传递给底层控件并让该控件发挥其魔力".我不是真的想要重新编码,只需传递鼠标点击(或其他任何东西)并让,在这种情况下,ListView会像上面第一个例子中那样做出反应.
Han*_*ant 52
Windows窗体设计器为大多数控件提供专用的设计器类.ListView的设计者是System.Windows.Forms.Design.ListViewDesigner,System.Design.dll程序集中的内部类.此类使您能够拖动列标题.
UserControl使用System.Windows.Forms.Design.ControlDesigner设计器类.它没有做任何特殊的事情,只需在控件周围放置一个带拖动手柄的矩形.您可以看到它的标题:在您将用户控件放在表单上之后,用于设计类的ControlDesigner,ListViewDesigner不在图片中.因此,您无法拖动列标题.另请注意,ControlDesigner不允许访问UC内的控件.
然而,通过创建自己的设计师,这是可以解决的.从Projects + Add Reference开始,选择System.Design.您需要向UC添加公共属性以公开列表视图并应用[DesignerSerializationVisibility]属性以允许保存更改的属性.并将[Designer]属性应用于UC类以替换默认设计器.这一切都应该类似于此(使用默认名称和显示"employees"的ListView):
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在可以单击并按常规设计用户控件中的列表视图.
| 归档时间: |
|
| 查看次数: |
10934 次 |
| 最近记录: |