views:

99

answers:

1

Let's say I have a custom activity that has a dependency property of type GUID.

I want in my custom designer to show like a combobox (or my own usercontrol) with possible values to select from (the values should comes from the database).

Is this possible ?

+2  A: 

You need to create a UITypeEditor. The following is a template for a combox editor:-

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;
  }
}

On your property in the activity:-

[Editor(typeof(MyCustomEditor), typeof(UITypeEditor)]
public Guid MyGuidValue
{
    get { return (Guid)GetValue(MyGuidValueProperty); }
    set { SetValue(MyGuidValueProperty, value); }
}
  • The Editor attribute will tell the PropertyGrid that you have created a custom editor for this property.
  • The GetEditStyle method of the Editor tells the property grid to display a drop down button on the propery value.
  • When clicked the property grid calls the custom editor's EditValue method.
  • The editor service is used to display a drop down with the DropDownControl method which takes a control that is to be display in the drop down area.
  • The DropDownControl method will block until the editor service CloseDropDown method is called.
AnthonyWJones
Thanks Anthony, I will try it out. Do you know if it's possible for a list checkbox? Or even My custom control?
pdiddy
You can pass pretty much whatever control you like to the DropDownControl including a custom one of your own.
AnthonyWJones