次の方法で共有


レイヤー図へのカスタム プロパティの追加

この Visual Studio 2010 Feature Pack を使用すると、Visual Studio 2010 Ultimate のレイヤー図の要素用のカスタム プロパティを定義できます。 カスタム プロパティは、ユーザーが表示して編集できるように、[プロパティ] ウィンドウに表示できます。 詳細については、「Visual Studio Feature Pack」を参照してください。

レイヤー要素 (ILayerElement) には、Name や Description などの複数の標準プロパティがあります。 これらのプロパティを、ユーザーは [プロパティ] ウィンドウで編集でき、プログラム コードでも読み取って更新できます。

独自のプロパティを定義し、任意のレイヤー要素と関連付けることができます。 そのための最も簡単な方法は、すべての ILayerElement にアタッチされている Properties ディクショナリに値を追加することです。 独自のプログラム コードでプロパティを使用するだけであれば、これで十分である場合があります。

ただし、カスタム プロパティを定義することで、[プロパティ] ウィンドウで追加の値をユーザーに対して表示することができます。 カスタム プロパティ記述子は PropertyExtension<T> を継承するクラスであり、T は ILayerElement またはいずれかの派生クラスです。 T はプロパティを定義する対象の型です。

たとえば、次の型でプロパティを定義できます。

  • ILayerModel - モデル

  • ILayer - 各レイヤー

  • ILayerDependencyLink - レイヤー間のリンク

  • ILayerComment

  • ILayerCommentLink

注意

任意の ILayerElement と共に文字列を格納するための簡単なメカニズムもあります。 各要素にアタッチされている Properties ディクショナリに値を格納できます。 これは、ユーザーが直接編集できないようにする必要のあるデータに便利です。 詳細については、「プログラム コードにおけるレイヤー モデル内の移動およびレイヤー モデルの更新」を参照してください。

次のコードは、標準的なカスタム プロパティ記述子です。 この例では、Boolean プロパティがレイヤー モデル (ILayerModel) で定義されており、ユーザーはこれを使用してカスタム検証メソッドに値を提供できます。

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;
      }
    }
  }
}

参照

その他の技術情報

レイヤー図に関する拡張機能の作成