In creating a custom workflow activity and trying to follow the advice on creating a validator, I ran into one issue which was that I have two types of activity properties: Those which need to be set at design-time and those which are dynamic or bound at runtime. The validator examples give little detail on how to differentiate the two. Here is *a* solution:
In your activity base class "CustomActivityBase", expose the property which will indicate when we are in design mode:
internal bool IsDesignMode { get { return this.DesignMode; } }
(This method calls a protected member, but we'll need to access it in the validator class)
Next, in the validator base class, we have to distinguish validating the activity in design mode, runtime and also for some reason the validator is also called when the activity itself is compiled. In your Validate method, you need to add the checks.
public override ValidationErrorCollection Validate(ValidationManager manager, object obj) {
ValidationErrorCollection errors = base.Validate(manager, obj);
//only do validation when the activity is in a workflowif (((CustomActivityBase)obj).Parent != null) {
if (((CustomActivityBase)obj).IsDesignMode) { implDesigntimeValidation(errors, manager, obj); }
else { implRuntimeValidation(errors, manager, obj); }
}
return errors;
}
Finally, notice that you can repeat these steps and perform this same validation on a workflow (although you don't need to check activity.Parent for null since it is the parent).