【发布时间】:2011-05-10 08:57:48
【问题描述】:
如何在我自己的自定义窗口控件库中实现小任务功能,如下所示?
【问题讨论】:
标签: c# custom-controls
如何在我自己的自定义窗口控件库中实现小任务功能,如下所示?
【问题讨论】:
标签: c# custom-controls
您需要为您的控件创建自己的设计器。通过添加对 System.Design 的引用来开始。示例控件可能如下所示:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
[Designer(typeof(MyControlDesigner))]
public class MyControl : Control {
public bool Prop { get; set; }
}
注意 [Designer] 属性,它设置自定义控件设计器。要开始您的设计,请从 ControlDesigner 派生出您自己的设计器。覆盖 ActionLists 属性为设计器创建任务列表:
internal class MyControlDesigner : ControlDesigner {
private DesignerActionListCollection actionLists;
public override DesignerActionListCollection ActionLists {
get {
if (actionLists == null) {
actionLists = new DesignerActionListCollection();
actionLists.Add(new MyActionListItem(this));
}
return actionLists;
}
}
}
现在您需要创建您的自定义 ActionListItem,它可能如下所示:
internal class MyActionListItem : DesignerActionList {
public MyActionListItem(ControlDesigner owner)
: base(owner.Component) {
}
public override DesignerActionItemCollection GetSortedActionItems() {
var items = new DesignerActionItemCollection();
items.Add(new DesignerActionTextItem("Hello world", "Category1"));
items.Add(new DesignerActionPropertyItem("Checked", "Sample checked item"));
return items;
}
public bool Checked {
get { return ((MyControl)base.Component).Prop; }
set { ((MyControl)base.Component).Prop = value; }
}
}
在 GetSortedActionItems 方法中构建列表是创建自己的任务项面板的关键。
这是快乐的版本。我应该注意到,在处理此示例代码时,我曾三次将 Visual Studio 崩溃到桌面。 VS2008 对自定义设计器代码中未处理的异常不具有弹性。经常保存。调试设计时代码需要启动另一个 VS 实例,它可以在设计时异常上停止调试器。
【讨论】:
【讨论】: