Adding Custom Properties to a Layer Diagram
This Visual Studio 2010 feature pack lets you define custom properties for elements on layer diagrams in Visual Studio 2010 Ultimate. You can have these properties appear in the Properties window, so that users can see and edit them. For more information, see Visual Studio Feature Packs.
Layer elements (ILayerElement) have several standard properties such as Name and Description. These properties can be edited by the user in the Properties window and can also be read and updated by your program code.
You can define your own properties that can be associated with any layer element. The simplest way to do this is to add values to the Properties dictionary that is attached to every ILayerElement. This might be sufficient if you only want to use the properties in your own program code.
However, by defining a custom property, you can make an additional value visible to the user in the Properties window. A custom property descriptor is a class that inherits from PropertyExtension<T> where T is ILayerElement or one of its derived classes. T is the type for which the property is defined.
For example, you can define properties on the following types:
ILayerModel - the model
ILayer - each layer
ILayerDependencyLink - the links between layers
ILayerComment
ILayerCommentLink
Note
There is also a simpler mechanism for storing strings along with any ILayerElement. You can put values into the Properties dictionary that is attached to each element. It is useful for data that you do not want the user to edit directly. For more information, see Navigating and Updating Layer Models in Program Code.
Example
The following code is a typical custom property descriptor. It defines a Boolean property on the layer model (ILayerModel) that lets the user provide values for a custom validation method.
using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Layer;
namespace MyNamespace
{
/// <summary>
/// Custom properties are added to the Layer Designer via a custom
/// Property Descriptor. We have to export this Property Descriptor
/// using MEF to make it available in the Layer Designer.
/// </summary>
[Export(typeof(IPropertyExtension))]
public class AllTypesMustBeReferencedProperty
: PropertyExtension<ILayerModel>
{
/// <summary>
/// Each custom property must have a unique name.
/// Usually we use the full name of this class.
/// </summary>
public static readonly string FullName =
typeof(AllTypesMustBeReferencedProperty).FullName;
/// <summary>
/// Construct the property. Notice the use of FullName.
/// </summary>
public AllTypesMustBeReferencedProperty()
: base(FullName)
{ }
/// <summary>
/// The display name is shown in the Properties window.
/// We therefore use a localizable resource.
/// </summary>
public override string DisplayName
{
get { return Strings.AllTypesMustBeReferencedDisplayName; }
}
/// <summary>
/// Description shown at the bottom of the Properties window.
/// We use a resource string for easier localization.
/// </summary>
public override string Description
{
get { return Strings.AllTypesMustBeReferencedDescription; }
}
/// <summary>
/// This is called to set a new value for this property. We must
/// throw an exception if the value is invalid.
/// </summary>
/// <param name="component">The target ILayerElement</param>
/// <param name="value">The new value</param>
public override void SetValue(object component, object value)
{
ValidateValue(value);
base.SetValue(component, value);
}
/// <summary>
/// Helper to validate the value.
/// </summary>
/// <param name="value">The value to validate</param>
private static void ValidateValue(object value)
{ }
public override Type PropertyType
{ get { return typeof(bool); } }
/// <summary>
/// The segment label of the properties window.
/// </summary>
public override string Category
{
get
{
return Strings.AllTypesMustBeReferencedCategory;
}
}
}
}