Workflow Foundation 4.0 has very good support for custom activity development. We can have our own custom activity with very good look and feel. When you define a custom activity, one of the main requirements is how to manage the associated properties in the property grid.
We can either use the TypeConverter or PropertyValueEditor for managing the properties and associated editors in PropertyGrid. TypeConverters are same as those in WPF. PropertyValueEditors are similar to the PropertyEditors in WPF, which provides custom editor for our property. In this article we will discuss about the PropertyValueEditor.
PropertyValueEditor
WF4.0 contains mainly three types of editors defined under System.Activities.Presentation.PropertyEditing namespace.
-
PropertyValueEditor
-
DialogPropertyValueEditor
-
ExtendedPropertyValueEditor
For our sample and understanding, I am using the DialogPropertyValueEditor with Simple implementation. In another article we will look into how we can use the DialogPropertyValueEditor to show a dictionary of values and also will have a discussion on another type of editors.
Template
For defining any kind of Editors, we need one Template file which define the look and feel appear in the Propertygrid and the class file contains the editor logic.
For our sample, I am using the following template with one textbox and an EditModeSwitchButton. We can define the DataTemplate in a ResourceDictionary file or as part of a UserControl.
<DataTemplate x:Key="FileBrowserInlineEditorTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding StringValue}"/>
<PropertyEditing:EditModeSwitchButton Grid.Column="1"/>
</Grid>
</DataTemplate>
PropertyValueEditor
Next is the implementation of the Custom PropertValueEditor. Custom propertyValueEditor should be inherited from one of the above three classes.
In our sample, we are inheriting our Custom PropertyValueEditor from the DialogPropertyValueEditor class. Specify the InlineEditorTemplate in the Constructor. In our example, FileEditorResources is the ResourceDictionary, where we defined the template for the PropertyValueEditor. Override the ShowDialog method and implement our custom PropertyValueEditor behavior.
In our example, we are using the OpenFileDialog to select an XML file.
class FileBrowserDialogPropertyValueEditor : System.Activities.Presentation.PropertyEditing.DialogPropertyValueEditor
{
private FileEditorResources res = new FileEditorResources();
public FileBrowserDialogPropertyValueEditor()
{
this.InlineEditorTemplate = res["FileBrowserInlineEditorTemplate"] as DataTemplate;
}
public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML files (*.xml)|*.xml";
ofd.Title = "Select XML file";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
propertyValue.StringValue = ofd.FileName;
}
}
}
Test Activity
Let us use our Custom PropertyValueEditor in our Custom Activity. Attach the Editor attribute with the custom activity property.
public class TestActivity:CodeActivity
{
private string myFile;
[Browsable(true)]
[Editor(typeof(FileBrowserDialogPropertyValueEditor), typeof(DialogPropertyValueEditor))]
public string MyFile
{
get
{
return myFile;
}
set
{
myFile = value;
}
}
protected override void Execute(CodeActivityContext context)
{
MessageBox.Show(MyFile);
}
}
Result
Drag and drop the custom activity to workflow and observe the properties associated with it. Our MyFile property will appear with a textbox & Selection button. Once we click on the button, it will open the File Open dialog with title as Select XML file and filtered by XML files.
Conclusion
Window workflow foundation 4.0 is having full support for custom activity development. In custom activity development, we are dealing with various kinds of properties. A good Property value editor will increase the user friendliness of the custom activity. Here, we discussed about a simple custom property value editor. We will look more on the editors and type converters in another article.