次の方法で共有


Visual Studio のドメイン固有言語では図形の基本モデルの状態を反映させることができます。

このトピックのコード例では Dsl プロジェクトの .cs ファイルに追加します。 各ファイルにこれらのステートメントが必要です :

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

デコレータの表示を制御する設定の図形のプロパティ マップ

プログラム コードを記述せずに DSL 定義の形状とドメイン クラス間のマッピングを設定してデコレータの表示を制御できます。 詳細については、次のトピックを参照してください。

プロパティとして図形の色とスタイルを公開します。

DSL 定義ではクラスの図形を右クリックし***** Add Exposed ***** をポイントし***** Fill Color ***** などの項目の 1 つがをクリックします。

図形にプログラム コードまたはユーザーのドメインとして設定できるプロパティがあります。 たとえばコマンドや規則のプログラム コードで設定するには作成できます :

shape.FillColor = System.Drawing.Color.Red;

プログラムの制御下でのみをユーザーによってプロパティの変数を定義する場合はユース ケース図の *** Fill Color *** など新しいドメインのプロパティを選択します。 次に[プロパティ] ウィンドウでfalse に *** Is Browsable *** または true に設定 *** Is UI Readonly *** を設定します。

色の変更スタイル規則を定義する場所はモデル要素のプロパティに依存します

外観をモデルの他の部分の図形の依存更新する規則を定義できます。 たとえばモデル要素のプロパティの図形の色を更新するモデル要素の規則を定義できます。 変更の規則の詳細については規則によって変更内容がモデル内に反映される を参照してください。

元に戻すコマンドを実行すると規則が起動されないためストア内に保持される更新のみ規則を使用する必要があります。 これは図形のサイズと表示などグラフィカルな機能を備えていません。 図形の機能を更新するには 非ストアのグラフィック機能の更新 を参照してください。

次の例では前のセクションで説明したようにドメインのプロパティとして FillColor を公開していることを前提とします。

[RuleOn(typeof(ExampleElement))]
  class ExampleElementPropertyRule : ChangeRule
  {
    public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
    {
      base.ElementPropertyChanged(e);
      ExampleElement element = e.ModelElement as ExampleElement;
      // The rule is called for every property that is updated.
      // Therefore, verify which property changed:
      if (e.DomainProperty.Id == ExampleElement.NameDomainPropertyId)
      {
        // There is usually only one shape:
        foreach (PresentationElement pel in PresentationViewsSubject.GetPresentation(element))
        {
          ExampleShape shape = pel as ExampleShape;
          // Replace this with a useful condition:
          shape.FillColor = element.Name.EndsWith("3") 
                     ? System.Drawing.Color.Red : System.Drawing.Color.Green;
        }
      }
    }
  }
  // The rule must be registered:
  public partial class OnAssociatedPropertyExptDomainModel
  {
    protected override Type[] GetCustomDomainModelTypes()
    {
      List<Type> types = new List<Type>(base.GetCustomDomainModelTypes());
      types.Add(typeof(ExampleElementPropertyRule));
      // If you add more rules, list them here. 
      return types.ToArray();
    }
  }

図形のプロパティの初期化に OnChildConfigured を使用します。

これが最初に作成されたときの図形のプロパティを設定するには図でクラスの部分定義のオーバーライド OnChildConfigured()。 図のクラスはDSL 定義で指定され生成されたコードは Dsl\Generated Code\Diagram.cs にあります。 次に例を示します。

  partial class MyLanguageDiagram
  {
    protected override void OnChildConfigured(ShapeElement child, bool childWasPlaced, bool createdDuringViewFixup)
    {
      base.OnChildConfigured(child, childWasPlaced, createdDuringViewFixup);
      ExampleShape shape = child as ExampleShape;
      if (shape != null) 
      {
        if (!createdDuringViewFixup) return; // Ignore load from file.
        ExampleElement element = shape.ModelElement as ExampleElement;
        // Replace with a useful condition:
        shape.FillColor = element.Name.EndsWith("3") 
            ? System.Drawing.Color.Red : System.Drawing.Color.Green;
      }
      // else deal with other types of shapes and connectors.
    }
  }

このメソッドは図形のサイズなどドメインのプロパティと非ストアの機能のための両方で使用できます。

図形の追加機能を更新するには AssociateValueWith () を使用します。

図形の一部の機能などまたはコネクタの矢印のスタイルでは影を表示するかどうか機能を公開する組み込みメソッドにドメインのプロパティとしてありません。 このような機能に対する変更は取引方法の制御下にあります。 ユーザーが元に戻すコマンドを実行すると規則が起動されないため規則を使用してファイルを更新することは適切ではありません。

代わりにOnAssociatedPropertyChanged を使用してこのような機能を更新できます。 次の例ではコネクタの矢印のスタイルはコネクタが次のように表示するリレーションシップのドメインのプロパティの値によって制御されています :

public partial class ArrowConnector // My connector class. 
{
   /// <summary>
    /// Called whenever a registered property changes in the associated model element.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnAssociatedPropertyChanged(VisualStudio.Modeling.Diagrams.PropertyChangedEventArgs e)
    {
      base.OnAssociatedPropertyChanged(e);
      // Can be called for any property change. Therefore,
      // Verify that this is the property we're interested in:
      if ("IsDirected".Equals(e.PropertyName))
      {
        if (e.NewValue.Equals(true))
        { // Update the shape’s built-in Decorator feature:
          this.DecoratorTo = LinkDecorator.DecoratorEmptyArrow;
        }
        else
        {
          this.DecoratorTo = null; // No arrowhead.
        }
      }
    }
    // OnAssociatedPropertyChanged is called only for properties
    // that have been registered using AssociateValueWith().
    // InitializeResources is a convenient place to call this.
    // This method is invoked only once per class, even though
    // it is an instance method. 
    protected override void InitializeResources(StyleSet classStyleSet)
    {
      base.InitializeResources(classStyleSet);
      AssociateValueWith(this.Store, Wire.IsDirectedDomainPropertyId);
      // Add other properties here.
    }
} 

AssociateValueWith() を登録する 1 回ドメインの各プロパティに対して呼び出されます。 が呼び出された後そのモデル要素を表すシェイプのプロパティはOnAssociatedPropertyChanged() に移動します。

各インスタンスに対してを呼び出す必要はAssociateValueWith()ありません。 InitializeResources はインスタンス メソッドであるが1 回だけシェイプの呼び出されます。