Monday, November 20, 2006

Windows Workflow - Activity Binding - The Long Road

This evening I had the opportunity to learn about a powerful feature within Windows Workflow. The feature is called "Activity Binding". The intent of this posting is to outline an efficient approach and provide some sample code for implementing Activity Binding within your workflow that houses a custom activity. Considering the intent of this blog is to provide more task based examples, I will author an article explaining what Activity Binding is, and why you would use it at a later date. However, this blog entry will explain the how.

In order to implement activity binding between a custom activity and your workflow, the following steps are necessary. It is assumed that you are creating the activity from scratch. However, you may skip to "Prepare your workflow" if you are simply trying to implement Activity Binding to a DependencyProperty.
  • Prepare your custom activity
    • You must define the property that you want to bind to as a DependencyProperty.

  • Prepare your workflow
    • In the .designer.cs file, add an ActvityBind entity for the custom activity's property that you exposed.
    • Set the Name property of the ActivityBind entity.
    • Set the Path property of the ActivityBind entity. The path is essentially name of the publicly visible property that you want to bind to.
    • Ensure that the custom activity has been instantiated within your workflow.
    • Call the SetBinding method of the instantiated custom activity to bind the property of the activity to the workflow.

Within your MyActivity.cs file, you will have something that looks like the following:

public static DependencyProperty NameProperty =
  DependencyProperty.Register("Name", typeof(String),
  typeof(MyActivity));

[Description("The description of NameProperty")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public string Name
{
  get
  {
    return (String)(base.GetValue(MyActivity.NameProperty));  }
  set
  {
    SetValue(MyActivity.NameProperty, value);
  }
}

Within the InitializeComponent() of your workflow (in the .designer.cs):

// Setup the binding information
ActivityBind nameActivityBind = new ActivityBind();
nameActivityBind.Name = "MyWorkflow";
nameActivityBind.Path = "Name";

// Setup the link between the workflow and the
// custom property of the custom activity
// Assume that this.activity is an instance of MyActivity
this.activity = new MyActivity();
this.activity.SetBinding(MyActivity.NameProperty,
  ((ActivityBind)(nameActivityBind)));

1 comment:

Ashwin said...

Hey Chad,

Nice Post,but could you please explain how to bind parameters between 2 custom activities without bringing in the workflow using the activity bind class

thanks,
Ashwin