Add multiple Property Metadata to a Dependency Property in a Workflow Activity

末鹿安然 提交于 2020-01-04 09:39:21

问题


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:


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.



来源:https://stackoverflow.com/questions/1728356/add-multiple-property-metadata-to-a-dependency-property-in-a-workflow-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!