Msdn上对DesignerActionList和DesignerAction的介绍为:DesignerAction 功能允许组件和控件显示区分大小写的信息和命令。DesignerAction 功能可被视为设计器谓词的替代项,因为 DesignerActionItem 可显示在智能标记面板中,也可显示在与组件或控件相关联的快捷菜单中。对于要在自定义组件和控件中添加智能标记支持的开发人员,DesignerActionList 类表示主交互点。DesignerActionList 是一个基类,组件开发人员可从中派生类来填充智能标记面板。智能标记面板将智能标记表示为类似于菜单的用户界面 (UI)。
DesignerActionItem为智能面板上的一项,我们要向智能面板上添加一项,只要实例化一个DesignerActionItem,DesignerActionList类里有个可以被override的方法GetSortedActionItems()返回类型为DesignerActionItemCollection,它实际上就是返回智能面板上项的集合,我们只要把DesignerActionItem实例添加到GetSortedActionItems()的返回值即可。
在.net中DesignerActionMethodItem、DesignerActionPropertyItem、DesignerActionTextItem、DesignerActionHeaderItem继承于DesignerActionItem,它们的成员大部分都是相同的,DesignerActionMethodItem主要在智能面板上生成一个方法项,点击这个项会去执行相应的方法;DesignerActionPropertyItem则把Component的属性显示在智能面板上,用户可以在职能面板上对Component的属性进行设置和修改;DesignerActionTextItem是在智能面板上生成一个文本项;而DesignerActionHeaderItem是继承于DesignerActionTextItem,但它多了分组的功能。
代码演示如下:
using System.Collections.Generic;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ClassLibrary1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
[Designer(typeof(CustomerDesigner), typeof(IDesigner))]
public class Customer : Component
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _id;
private Sex _sex;
private string _address;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Id
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Sex Sex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _sex; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _sex = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Address
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _address; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _address = value; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum Sex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
男 = 0,
女 = 1
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class CustomerDesigner : ComponentDesigner
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private DesignerActionListCollection actionLists;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 只有get,没有set。
public override DesignerActionListCollection ActionLists
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (null == actionLists)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
actionLists = new DesignerActionListCollection();
actionLists.Add(
new CustomerActionList(this.Component));
}
return actionLists;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void OnClick2(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("Click2.");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 添加了一个谓词(在右键菜单中出现)。
// 如果重写了DesignerActionList,则此谓词不会加在智能标记中。
public CustomerDesigner()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DesignerVerb verb2 = new DesignerVerb("Click2", new EventHandler(OnClick2));
this.Verbs.Add(verb2);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// DesignerActionList:智能标记面板上的项列表
public class CustomerActionList : DesignerActionList
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Customer customer;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public CustomerActionList(IComponent component) : base(component)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.customer = component as Customer;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
TypeDescriptor —— 通过类型来获取Component的Attribute、Property、Event。
*/
public string Id // 注1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return customer.Id; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ TypeDescriptor.GetProperties(typeof(Customer))["Id"]
.SetValue(customer, value);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Sex Sex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return customer.Sex; }
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ TypeDescriptor.GetProperties(typeof(Customer))["Sex"]
.SetValue(customer, value); }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void OnClick1()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("Click1.");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override DesignerActionItemCollection GetSortedActionItems()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DesignerActionItemCollection items = new DesignerActionItemCollection();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// HeaderItem
// 创建智能面板上项的分类。
DesignerActionHeaderItem hInfo = new DesignerActionHeaderItem("Customer's Info", "Info");
items.Add(hInfo);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DesignerActionHeaderItem hAction = new DesignerActionHeaderItem("Customer's Action", "Action");
items.Add(hAction);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DesignerActionHeaderItem hDescription = new DesignerActionHeaderItem("Description");
items.Add(hDescription);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// PropertyItem
// DesignerActionPropertyItem("Id" —— 注1位置的Property
// , "Id" —— 显示在智能面板上的名称
// , "Info" —— 智能面板上的分类
// ,"Customer's id") —— 补充说明文本
items.Add(new DesignerActionPropertyItem("Id", "Id", "Info", "Customer's id"));
items.Add(new DesignerActionPropertyItem("Sex", "Sex", "Info", "Customer's sex"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 添加一个MethodItem。
// 此MethodItem会默认向Component的右键菜单中添加一项。
items.Add(new DesignerActionMethodItem(this, "OnClick1", "Click1", "Action", true));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 添加一个TextItem.
items.Add(new DesignerActionTextItem("http://mapserver.cnblogs.com", "Description"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return items;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
效果如下:
![](https://images.cnblogs.com/cnblogs_com/mapserver/7-0.jpg)
![](https://images.cnblogs.com/cnblogs_com/mapserver/7-1.jpg)
如果大家有什么问题,请给我留言、或者评论。