A Declarative Condition Activity
Microsoft Activites provided in the Workflow namespace that have condition properties support both declarative and code conditions. An example of this is the WhileActivity.

This flexibilty may not be desired. Especially if you are hosting the WorkflowDesigner for business people to modify workflows. Business people and c# do not mix.
We can handle this by defining our condition property to use a custom UITypeEditor that will handle the assigning of a condition name and using System.Workflow.Activities.Rules.Design.RuleConditionDialog . Luckily this type is public. Microsoft had a fetish for internal sealed when creating many of the types in the Workflow namespace. We reference the UITypeEditor by applying an attribute on our Condition property
[Editor(typeof(RuleRefTypeEditor), typeof(UITypeEditor))]
public string Condition
{
In this example I use a string type to hold a reference to the condition that will be stored in the workflows RuleDefinitions . If I wanted to follow Microsoft’s pattern then I would subclass the ActivityCondition and provide a TypeConverter that would call our custom UITypeEditor. But for simplicity I’ve removed two levels of separation. The main work is done in the RuleRefTypeEditor type.
public string Condition
{
In this example I use a string type to hold a reference to the condition that will be stored in the workflows RuleDefinitions . If I wanted to follow Microsoft’s pattern then I would subclass the ActivityCondition and provide a TypeConverter that would call our custom UITypeEditor. But for simplicity I’ve removed two levels of separation. The main work is done in the RuleRefTypeEditor type.
public class RuleRefTypeEditor:UITypeEditor
{
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(
System.ComponentModel.ITypeDescriptorContext context)
{
return System.Drawing.Design.UITypeEditorEditStyle.Modal;
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
System.CodeDom.CodeExpression expression = null;
RuleDefinitions definitions = null;
IWindowsFormsEditorService editorService; // need ref to Windows.forms.dll
IReferenceService service;
Activity activityBeingEdited;
CompositeActivity workflow;
string selectedName = value as string;
if (selectedName == null)
{
selectedName = Guid.NewGuid().ToString();
}
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
service = provider.GetService(typeof(IReferenceService)) as IReferenceService;
activityBeingEdited = service.GetComponent(context.Instance) as Activity;
workflow = Helpers.GetRootActivity(activityBeingEdited) as CompositeActivity;
definitions = (RuleDefinitions)workflow.GetValue(RuleDefinitions.RuleDefinitionsProperty);
if (definitions == null)
{
definitions = new RuleDefinitions();
workflow.SetValue(RuleDefinitions.RuleDefinitionsProperty, definitions);
}
if (definitions.Conditions.Contains(selectedName))
{
RuleExpressionCondition conditionExpression = definitions.Conditions[selectedName] as RuleExpressionCondition;
if (conditionExpression != null)
{
expression = conditionExpression.Expression;
}
}
using (System.Workflow.Activities.Rules.Design.RuleConditionDialog dialog = new RuleConditionDialog(workflow, BLOCKED EXPRESSION
{
if (DialogResult.OK == editorService.ShowDialog(dialog))
{
CodeExpression expr = dialog.Expression;
if (definitions.Conditions.Contains(selectedName))
{
definitions.Conditions.Remove(selectedName);// removing because the indexer returns a clone not an actual reference
}
RuleExpressionCondition condition=new RuleExpressionCondition(selectedName,expr);
definitions.Conditions.Add(condition);
}
}
return selectedName;
}
I am assigning a guid to the condition name to simplify the experience for the non developer using this activity. If we wanted to provide the same experience that a WhileActivity,IfElseActivity, CAG then we would need to provide an editor to select,rename,delete conditions since Microsoft decided to … drum roll please.. yes that’s it. They declared it as internal sealed. So now we a declarative only condition property.
>
So now we have the ability to create conditions and they will be persisted with the Workflow. All that is left to do is override the Execute method on our activity to evaluate the condition.
Source Code