Filtering Child Activities on a Composite Activity

Link. September 10, 2006. Comments [0]. Posted in: WinFX | Workflow

If you're creating a custom composite activity, you might need to restrict whether a given activity can be inserted as a child at a specific point of your composite activity when working on the Workflow Designer. Fortunately, this is actually fairly easy using the designer facilities in Windows Workflow Foundation.

Let's quickly sample this by building a custom CodeFirstSequentialActivity, which is a simple sequential composite activity that requires that the first child activity in the sequence is a CodeActivity. Other than that, any other type of activity can be a child.

First, we'll create a custom designer class for our activity, which we'll inherit from the SequenceDesigner class. In it, we'll override the CanInsertActivities method, in which we'll do a simple test: If the insertion point is the first one on our composite activities, then we'll check that the first activity being dropped/inserted is indeed a CodeFirstActivity, and, if so, just return false to block the designer from inserting the child activities:

public class CodeFirstSequentialActivityDesigner : SequenceDesigner
{
   public override bool CanInsertActivities(HitTestInfo insertLocation,
      System.Collections.ObjectModel.ReadOnlyCollection<Activity> activitiesToInsert)
   {
      bool canDo = base.CanInsertActivities(insertLocation, activitiesToInsert);
      CompositeActivity theActivity = Activity as CompositeActivity;
      if ( theActivity != null )
      {
         // first activity in can only be a CodeActivity
         if ( insertLocation.MapToIndex() == 0 )
         {
            if ( !(activitiesToInsert[0] is CodeActivity) )
            {
               return false;
            }
         }
      }
      return canDo;
   }
}

Then, it's just a matter of associating our custom designer with our activity:

[Designer(typeof(CodeFirstSequentialActivityDesigner))]
class CodeFirstSequentialActivity : CompositeActivity
{
   // ...
}

A nice thing about this is that this will block Drag & Drop operations as well as custom trying to copy/paste activities into our custom composite activities, without having to handle both operations as different things.

One thing to watch out, though, is that this only takes care of the designer. You'll also want to create a custom activity validator that validates that child activities are of the right type.



Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1051
This Year: 2
This Month: 2
This Week: 1
Comments: 827

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In