Using Dependency Properties 

Dependency properties provide a centralized repository of a workflow's state. The DependencyObject is basically a hash table that stores the current values of any DependencyProperty that is ever applied to it.

An instance type of dependency property can be bound to instance data, in which case the actual value is not determined until run time. You set this type of dependency property's value to be an ActivityBind to bind to the actual value that is accessed at run time.

ActivityBind binds an activity property to any of the following:

  • Another activity property

  • A field

  • A property

  • A method

When you are binding to an activity property, if the activity on which the binding is set (the target) is a custom activity, Windows Workflow Foundation tries to resolve the source activity starting from the target's parent activity. If the source activity is not found, Windows Workflow Foundation tries to resolve the name starting from the target activity itself. If the target activity is not a custom activity, Windows Workflow Foundation tries to resolve the source activity starting from the target activity itself.

A meta type of dependency property must be set to a literal value at design time because it is immutable at run time.

NoteNote

Any values set in ValidationOptionAttribute are ignored for instance-based dependency properties; however, they are valid for meta-based dependency properties.

NoteNote

If you create an activity property whose type is derived from DependencyObject, it must be declared as a meta dependency property or an exception is thrown by the workflow runtime engine during runtime.

When you create names for your dependency properties, you must choose unique names that are not being used for dependency properties or events in any base classes that you inherit from; otherwise, an ArgumentException is thrown during run time.

DependencyProperty Example

The following code example shows how to implement a condition using a DependencyProperty. The example shows how to add a DependencyProperty, with the correct type passed in Register.

public static DependencyProperty ExecutionConditionProperty = DependencyProperty.Register("ExecutionCondition", typeof(System.Workflow.ComponentModel.ActivityCondition), typeof(ActivityLibrary2.Activity1));

[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
[BrowsableAttribute(true)]
public System.Workflow.ComponentModel.ActivityCondition ExecutionCondition
{
    get
    {
        return ((System.Workflow.ComponentModel.ActivityCondition)(base.GetValue(ActivityLibrary2.Activity1.ExecutionConditionProperty)));
    }
    set
    {
        base.SetValue(ActivityLibrary2.Activity1.ExecutionConditionProperty, value);
    }
}

Attached Properties

In certain cases, you need an activity to register a property that can be used by other activities in a workflow. This kind of property is called an attached property, which is a specialized form of dependency property. You register an attached property using the RegisterAttached method of the DependencyProperty class.

An attached property is a type of a dependency property that other classes declare that can then be applied to any subclass of the DependencyObject class. The class that owns the dependency property must provide static accessor methods to get and set the value on arbitrary DependencyObject instances.

The following code shows how you can use an attached property to enable child activities of a composite activity to store unique values of a property that is defined in the parent activity.

public sealed class CustomActivity : CompositeActivity, IActivityEventListener<ActivityExecutionStatusChangedEventArgs>
{
    // Declare attached property provided to the child actvities.
    public static readonly DependencyProperty WhenConditionProperty = 
        DependencyProperty.RegisterAttached("WhenCondition", typeof(ActivityCondition), typeof(CustomActivity), new PropertyMetadata(DependencyPropertyOptions.Metadata));

    // Constructors
    public CustomActivity()
    {
    }

    public CustomActivity(string name)
        : base(name)
    {
    }

    //**********************************************************************
    // Get and Set accessors for the attached property WhenConditionProperty.
    //**********************************************************************
    public static object GetWhenCondition(object dependencyObject)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException("dependencyObject");
        return (dependencyObject as DependencyObject).GetValue(WhenConditionProperty);
    }

    public static void SetWhenCondition(object dependencyObject, object value)
    {
        if (dependencyObject == null)
            throw new ArgumentNullException("dependencyObject");
        (dependencyObject as DependencyObject).SetValue(WhenConditionProperty, value);
    }

    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {
        // Execute implementation omitted for clarity.
    }

    public void OnEvent(object sender, ActivityExecutionStatusChangedEventArgs e)
    {
        // OnEvent implementation omitted for clarity.
    }

    private void EvaluateChildConditions(Activity child, ActivityExecutionContext context)
    {
            ActivityCondition whenCondition = (ActivityCondition)child.GetValue(CustomActivity.WhenConditionProperty);
            // ...
    }
}

For more information about dependency properties, see the DependencyProperty and DependencyObject classes of the System.Workflow.ComponentModel namespace in the Windows Workflow Foundation Class Library reference.

See Also

Reference

DependencyObject
DependencyProperty

Concepts

Using Activity Properties
Creating Custom Activities

Footer image

Send comments about this topic to Microsoft.