3

假设我有一个自定义活动,它具有 GUID 类型的依赖属性。

我希望在我的自定义设计器中显示为一个组合框(或我自己的用户控件),其中包含可供选择的值(值应该来自数据库)。

这可能吗 ?

4

1 回答 1

3

您需要创建一个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 您已为此属性创建了自定义编辑器。
  • Editor的GetEditStyle方法告诉属性网格在属性值上显示一个下拉按钮。
  • 单击属性网格时,将调用自定义编辑器的EditValue方法。
  • 编辑器服务用于显示下拉菜单,该DropDownControl方法采用要在下拉区域中显示的控件。
  • DropDownControl方法将阻塞,直到CloseDropDown调用编辑器服务方法。
于 2009-11-24T11:18:05.503 回答