ImageField Class

Lightweight object that represents a part of a shape that can display image decorators or backgrounds.

Namespace:  Microsoft.VisualStudio.Modeling.Diagrams
Assembly:  Microsoft.VisualStudio.Modeling.Sdk.Diagrams (in Microsoft.VisualStudio.Modeling.Sdk.Diagrams.dll)

Syntax

'Declaration
Public Class ImageField _
    Inherits ShapeField
'Usage
Dim instance As ImageField
public class ImageField : ShapeField
public ref class ImageField : public ShapeField
public class ImageField extends ShapeField

Remarks

When you define an image decorator in a shape, and when you define an image shape, the area in which the shape is displayed is managed by an ImageField. For examples of the initialization of ImageFields and other ShapeFields, inspect Dsl\GeneratedCode\Shapes.cs in your DSL solution.

An ImageField is an object that manages an area within a shape, such as the space assigned to a decorator. One ImageField instance is shared between many shapes of the same shape class. The ImageField instance does not store a separate image for each shape: instead, the GetDisplayImage(ShapeElement) method takes the shape as a parameter, and can look up the image dependent on the current state of the shape and its model element.

If you want special behavior such as a variable image, you can create your own class derived from ImageField.

To create a subclass of ImageField

  1. Set the Generates Double Derived property of the parent shape class in your DSL Definition.

  2. Override the InitializeShapeFields method of your shape class.

    • Create a new code file in the DSL project, and write a partial class definition for the shape class. Override the method definition there.
  3. Inspect the code of InitializeShapeFields in DSL\GeneratedCode\Shapes.cs.

    In your override method, call the base method and then create an instance of your own image field class. Use this to replace the regular image field in the shapeFields list.

Examples

This example makes an icon change dependent on the state of the shape’s model element.

Warning

This example demonstrates how to make a dynamic image decorator. But if you only want to switch between one or two images depending on the state of a model variable, it is simpler to create several image decorators, locate them in the same position on the shape, and then set the Visibility filter to depend on specific values of the model variable. To set this filter, select the shape map in the DSL Definition, open the DSL Details window, and click the Decorators tab.

To run this example code, create a new DSL solution using the Minimal Language template. Add a Boolean domain property AlternateState to the ExampleElement domain class. Add an icon decorator to the ExampleShape class, and set its image to a bitmap file. Click Transform All Templates. Add a new code file in the DSL project, and insert the following code.

To test the code, press F5 and, in the debugging solution, open a sample diagram. The default state of the icon should appear. Select the shape and in the Properties window, change the value of the AlternateState property. The icon should then appear rotated through 90 degrees, on that shape.

using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
...
partial class ExampleShape
{
    /// <summary>
    /// Compose a list of the fields in this shape.
    /// Called once for each shape class.
    /// </summary>
    /// <param name="shapeFields"></param>
    protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
    {
      // Fields set up according to DSL Definition:
      base.InitializeShapeFields(shapeFields);

      // Replace the image field:
      ShapeField oldField = ShapeElement.FindShapeField(shapeFields, "IconDecorator");
      shapeFields.Remove(oldField);
      // Must keep the same name:
      MyImageField newField = new MyImageField(oldField.Name);
      shapeFields.Add(newField);
      newField.DefaultImage = (oldField as ImageField).DefaultImage.Clone() as System.Drawing.Image;
    }
  }


  public class MyImageField : ImageField
  {
    public MyImageField(string tag) : base(tag) { }

    /// <summary>
    /// Get the image for this field in the given shape.
    /// </summary>
    public override System.Drawing.Image GetDisplayImage(ShapeElement parentShape)
    {
      ExampleElement element = parentShape.ModelElement as ExampleElement;
      if (element.AlternateState == true)
        return AlternateImage;
      else
        return base.GetDisplayImage(parentShape);
    }

    private System.Drawing.Image alternateImage;
    public System.Drawing.Image AlternateImage
    {
      get
      {
        if (alternateImage == null)
        {
          // Alternate image is a copy of the default, rotated by 90 degrees:
          alternateImage = this.DefaultImage.Clone() as System.Drawing.Image;
          alternateImage.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
        }
        return alternateImage;
      }
    }
  }
}

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Modeling.Diagrams.ShapeField
    Microsoft.VisualStudio.Modeling.Diagrams.ImageField

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

ImageField Members

Microsoft.VisualStudio.Modeling.Diagrams Namespace

TextField

ShapeField

InitializeShapeFields(IList<ShapeField>)

Change History

Date

History

Reason

Added example, improved description.

Customer feedback.