假设我有一个自定义活动,它具有 GUID 类型的依赖属性。
我希望在我的自定义设计器中显示为一个组合框(或我自己的用户控件),其中包含可供选择的值(值应该来自数据库)。
这可能吗 ?
假设我有一个自定义活动,它具有 GUID 类型的依赖属性。
我希望在我的自定义设计器中显示为一个组合框(或我自己的用户控件),其中包含可供选择的值(值应该来自数据库)。
这可能吗 ?
您需要创建一个UITypeEditor
. 以下是组合框编辑器的模板:-
public class MyCustomEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider)
{
var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
var list = new ListBox();
// Your code here to populate the list box with your items
EventHandler onclick = (sender, e) => {
editiorService.CloseDropDown();
};
list.Click += onclick;
myEditorService.DropDownControl(list);
list.Click -= onclick;
return (list.SelectedItem != null) ? list.SelectedItem : Guid.Empty;
}
}
在活动中您的财产:-
[Editor(typeof(MyCustomEditor), typeof(UITypeEditor)]
public Guid MyGuidValue
{
get { return (Guid)GetValue(MyGuidValueProperty); }
set { SetValue(MyGuidValueProperty, value); }
}
Editor
属性将告诉 PropertyGrid 您已为此属性创建了自定义编辑器。 GetEditStyle
方法告诉属性网格在属性值上显示一个下拉按钮。EditValue
方法。DropDownControl
方法采用要在下拉区域中显示的控件。DropDownControl
方法将阻塞,直到CloseDropDown
调用编辑器服务方法。