Constraining what activities can be dropped on a custom activity designer in Windows Workflow Foundation 4

When designing a custom activity that can be used as a container for other activities, you may want to restrict the activity user to a certain set of activities.

If the custom activity can only accept a single type, this can be implemented quite simply by setting the WorkflowItemPresenter.AllowedItemType or WorkflowItemsPresenter.AllowedItemType property. In this example, I allow only a WriteLine activity to be dropped:

<sap:WorkflowItemsPresenter HintText="Drop Activities Here" 
Margin="5" MinHeight="100"
Items="{Binding Path=ModelItem.Activities}"
AllowedItemType="{x:Type TypeName=sa:WriteLine}">

Note that to use the above code, you'll need to add a namespace declaration to your designer:


If you need to restrict to a set of allowed activity types, rather than a single type, this can be done by adding a validation error to the validation errors collection during the CacheMetadata phase. Note that this will create a runtime error (though technically the user will experience it in the design phase), which will cause an error icon to be displayed, rather than preventing the activity to be dropped in the first place. In this example, I allow only a WriteLine or Assign activity to be dropped:

protected override void CacheMetadata(NativeActivityMetadata metadata)
foreach (Activity a in Activities)
if ((a.GetType() != typeof(System.Activities.Statements.WriteLine)) && (a.GetType() != typeof(System.Activities.Statements.Assign)))
metadata.AddValidationError("Child activity is not of type WriteLine or Assign");

Comments (1)
  1. If you only allow a single child activity, then you can also achieve this by defining the body property as a specific type.

    For example:



           public Receive Body





    See…/76140 for the full class.

Comments are closed.

Skip to main content