共用方式為


Constraining what activities can be dropped on a custom activity designer in Workflow 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. Here is an example of this:

<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:

xmlns:sa="clr-namespace:System.Activities.Statements;assembly=System.Activities"

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. The following example shows how to create a validation error when an activity is added to the Activities collection that is not of type WriteLine or Assign:

protected override void CacheMetadata(NativeActivityMetadata metadata){    base.CacheMetadata(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");        }    }}