智能标签,提供快捷的控件属性编辑。毋庸多说,见下图1:
如何在自定义的控件里也添加上SmartTag呢?本文作简单描述。
秘密就在自定义控件的Designer里面。
ControlDesigner. ActionLists 属性用以实现具体的SmartTag。
首先需要写一个从DesignerActionList派生的类用来实现我们的SmartTag,本文只在SmartTag上放了一个TextBox
看到我们加了一个DesignerActionPropertyItem用以编辑Text,
public DesignerActionPropertyItem(
string memberName,
string displayName,
string category,
string description
)
参数memberName,变量名
参数displayName,显示的名称
参数category,类别名称
参数description,描述,在ToolTip中显示
注意到我给它的Category传的是Appearance,为了好看我想给它加个类别,怎么加呢?如下:
item.Add(new DesignerActionHeaderItem("Appearance"));
显然Text是个Property,我需要告诉SmartTag怎么得到Text的值,怎么把编辑的值送回去,在CustomControlActionList类中创建一个“Text”属性就可以了
设值是通过反射去做的。
好了,接下来我们要做的事情就是把它添加到ControlDesigner中了
很简单吧,好了看看我们的CustomControl效果:
给出所有的代码:
CustomControl1
CustomControlDesigner及CustomControlActionList