Using the ModelItem Editing Context

The ModelItem editing context is the object that the host application uses to communicate with the designer. EditingContext exposes two methods, Items and Services, which can be used

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:

  1. GetValue

  2. Subscribe

  3. Unsubscribe

  4. SetValue

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:

  1. Publish

  2. Subscribe

  3. Unsubscribe

  4. GetService

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:

Services: