Now that the control has been introduced it is time for the first designer. This designer will provide the bare necessities for designing a templated control. When used, the designer will display the DuoView control on the designer surface as displayed in Figure 1.
|
Figure 1. The simple designer used on the DuoView control |
The only task this designer needs to perform is allowing the user to edit the templates of the control. When accomplished the designer framework will provide two ways through which the templates can be edited. A menu item is created in the design-time shortcut menu and the default smart-tag can be used. The following image displays these two methods. Being a simple designer, no rendering logic is provided yet. This feature will be added in the next stage.
| |
| Figure 2. Selecting a template for editing using the shortcut menu or smart-tag |
Defining and initializing a designer
To construct this designer, you need to derive a class from ControlDesigner. This class will be handed a reference to the control on the designer surface using an override of the Initialize method. The override will accomplish one part of the task required for the designer by indicating that a templated control will be designed.
public class DuoViewDesignerBase : ControlDesigner
{ public override void Initialize(IComponent component)
{ if (!(component is DuoView))
{ throw new ArgumentException("Component is not a DuoView."); }
base.Initialize(component);
base.SetViewFlags(ViewFlags.TemplateEditing, true);
}
}
Figure 3. Initializing the designer
One should not forget to call the base implementation of the Initialize method. The base class implementation provides access to the control on the designer through the Component property. Before actually initializing the designer, you should ensure that the designer is applied to the right type of control. Typecasting the Component property to a DuoView control could otherwise result in runtime exceptions.
Providing information about templates
The initialization is complete and the designer host knows that DuoView is a templated control. Next you need to provide information about which templates are available in the DuoView. You supply this information by overriding the TemplateGroups property.
public override TemplateGroupCollection TemplateGroups
{ get
{ TemplateGroupCollection groups = base.TemplateGroups;
TemplateGroup displayGroup =
new TemplateGroup("FirstTemplate"); displayGroup.AddTemplateDefinition(
new TemplateDefinition(this, "First Template",
Component, "FirstTemplate"));
groups.Add(displayGroup);
TemplateGroup editGroup =
new TemplateGroup("SecondTemplate"); editGroup.AddTemplateDefinition(
new TemplateDefinition(this, "Second Template",
Component, "SecondTemplate"));
groups.Add(editGroup);
return groups;
}
}
Figure 4. Obtaining information about available templates
The TemplateGroups property provides information about which templates are available and in which groups they reside. A definition is identified by the TemplateDefinition class and a group by the TemplateGroup class. A TemplateGroup is a container for TemplateDefinitions. You can edit a group as a whole or you can select and individual template for editing. Figure 5 displays the editing interface provided by the design environment.
| |
| Figure 5. The interface which is displayed when editing a single template and when editing a group containing two templates |
The TemplateGroups property is accessed by the design environment many times during the lifecycle of a designer. It is, for instance, accessed before opening the shortcut menu. This makes it possible to dynamically change the provided template definitions at design-time.
So far the first designer. The designer class meets the basic requirements for designing a templated control using a minimal amount of code. A mayor drawback is the look of the control on the designer surface. The next step will add design-time rendering capabilities.