Using the ModelItem Editing Context
This topic applies to Windows Workflow Foundation 4 (WF4).
The ModelItem editing context is the object that the host application uses to communicate with the designer. EditingContext exposes two methods, Items and Services.
The Items collection
The Items collection is used to access data that is shared between the host and the designer, or data that is available to all designers. This collection has the following capabilities, accessed via the ContextItemManager class:
The Services collection
The Services collection is used to access services that the designer uses to interact with the host, or services that all designers use. This collection has the following methods of note:
Assigning a designer an activity
To specify which designer an activity uses, the Designer attribute is used.
[Designer(typeof(MyClassDesigner))]
public sealed class MyClass : CodeActivity
{
Creating a service
To create a service that serves as a conduit of information between the designer and the host, an interface and an implementation must be created. The interface is used by the Publish method to define the members of the service, and the implementation contains the logic for the service. In the following code example, a service interface and implementation are created.
public interface IMyService
{
IEnumerable<string> GetValues(string DisplayName);
}
public class MyServiceImpl : IMyService
{
public IEnumerable<string> GetValues(string DisplayName)
{
return new string[] {
DisplayName + " One",
DisplayName + " Two",
"Three " + DisplayName
} ;
}
}
Publishing a service
For a designer to consume a service, it must first be published by the host using the Publish method.
this.Context.Services.Publish<IMyService>(new MyServiceImpl);
Subscribing to a service
The designer obtains access to the service using the Subscribe method in the OnModelItemChanged method. The following code snippet demonstrates how to subscribe to a service.
protected override void OnModelItemChanged(object newItem)
{
if (!subscribed)
{
this.Context.Services.Subscribe<IMyService>(
servInstance =>
{
listBox1.ItemsSource = servInstance.GetValues(this.ModelItem.Properties["DisplayName"].ComputedValue.ToString());
}
);
subscribed = true;
}
}
Sharing data using the Items collection
Using the Items collection is similar to using the Services collection, except that SetValue is used instead of Publish. This collection is more appropriate for sharing simple data between the designers and the host, rather than complex functionality.
EditingContext host items and services
The .Net Framework provides a number of built-in items and services accessed through the editing context.
Items:
AssemblyContextControlItem: Manages the list of referenced local assemblies that will be used inside the workflow for controls (such as the expression editor).
ReadOnlyState: Indicates whether the designer is in a read-only state.
Selection: Defines the collection of objects that are currently selected.
WorkflowFileItem: Provides information on the file that the current editing session is based on.
Services:
AttachedPropertiesService: Allows properties to be added to the current instance, using AddProperty.
DesignerView: Allows access to the properties of the designer canvas.
IActivityToolboxService: Allows the contents of the toolbox to be updated.
ICommandService: Used to integrate designer commands (such as Context Menu) with custom-provided service implementations.
IDesignerDebugView: Provides functionality for the designer debugger.
IExpressionEditorService: Provides access to the Expression Editor dialog.
IIntegratedHelpService: Provides the designer with integrated help functionality.
IValidationErrorService: Provides access to validation errors using ShowValidationErrors.
IWorkflowDesignerStorageService: Provides an internal service to store and retrieve data. This service is used internally by the .Net Framework, and is not intended for external use.
IXamlLoadErrorService: Provides access to the XAML load error collection using ShowXamlLoadErrors.
ModelService: Used by the designer to interact with the model of the workflow being edited.
ModelTreeManager: Provides access to the root of the model item tree using Root.
UndoEngine: Provides undo and redo functionality.
ViewService: Maps visual elements to underlying model items.
ViewStateService: Stores view states for model items.
VirtualizedContainerService: Used to customize the virtual container UI behavior.
WindowHelperService: Used to register and unregister delegates for event notifications. Also allows a window owner to be set.