views:

539

answers:

1

Hi,

I am building a number of Custom Activities in Windows Workflow and I need to add a DependencyProperty which can list a number of values for that property which the user can then select when using the activity.

e.g. True or False.

I know how to simply pass a default using the PropertyMetadata, and presume that I will have to pass a list/class now the PropertyMetadata?

Has anyone already got an example of how to do this please?

(Example Code below)

public static DependencyProperty TestProperty = DependencyProperty.Register("Test", typeof(string), typeof(CheckActivity), new PropertyMetadata("True"));
/// <summary>
/// Dependency property for 'TestProperty'
/// </summary>   
[DescriptionAttribute("Whether a True/False entry is required")]
[CategoryAttribute("Settings")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
public string Type
{
    get
    {
        return ((string)(base.GetValue(CheckActivity.TestProperty)));
    }
    set
    {
        base.SetValue(CheckActivity.TestProperty, value);
    }
}
+1  A: 

First of all the True/False example isn't great, in that case use a bool type.

For a multi-value item why not use an Enum:-

 public enum ItemEnum
 {
    First,
    Second,
    Third
 }

Now in your Activity:-

 public static DependencyProperty TestProperty = DependencyProperty.Register("Test",  
   typeof(ItemEnum), typeof(TestActivity), new PropertyMetadata(ItemEnum.First));

[Description("Select Item value")]
[Category("Settings")]
[DefaultValue(ItemEnum.First)]
public ItemEnum Type
{
  get
  {
    return (ItemEnum)GetValue(TestActivity.TestProperty);
  }
  set
  {
    SetValue(TestActivity.TestProperty, value);
  }
}

Note the simplification of the Attributes on the property. In particular Browseable being true and DesignerSerializationVisiblity being Visible are defaults so remove them. Also the property grid is easier for the "user" to use if the DefaultValue is defined. Note also dropped the "Attribute" suffix, makes it much more straightforward to read.

AnthonyWJones
Sorry about the True/False, I actaully needed it for the words Automatic/Manual so your Enum example is very useful. Thanks for the points about removing the defaults. Wasnt sure if this was going to get answered, as Workflow knowledge is a bit week on here. Again Thanks!!!
kevchadders
+1 and the correct answer.
kevchadders