Behaviors and actions

Microsoft Expression Blend 4 defines a standard for behaviors, which are reusable pieces of packaged code that can be dragged onto any object and fine-tuned by changing their properties. Behaviors allow you to add interactivity to your applications without having to write any code. The Behavior API consists of three core classes: Trigger, Action, and Behavior. The ArcGIS API for Silverlight includes a set of behaviors and actions in the ESRI.ArcGIS.Client.Behaviors assembly that can be used to define interactive relationships between user input and map behavior and content.

NoteNote:

To use behaviors and actions in your application, you must add a reference to the System.Windows.Interactivity assembly. This assembly is included with the Expression Blend 4 product or the Expression Blend 4 SDK (currently included with Expression Blend 4).

In all code examples below, references to the System.Windows.Interactivity, ESRI.ArcGIS.Client, and ESRI.ArcGIS.Client.Behaviors assemblies were added to the project, and a namespace reference for each was added to the page:

xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:esriBehaviors="clr-namespace:ESRI.ArcGIS.Client.Behaviors;assembly=ESRI.ArcGIS.Client.Behaviors"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Behaviors

A behavior is composed of a trigger and an action for a specific control. All behaviors included with the ArcGIS API for Silverlight are designed to work with a predefined event on the map (trigger) that generates (action) a result. The behaviors are packaged for you; therefore, you only need to add a behavior to a map to use it. The table below lists the map behaviors, provides a description, and shows a code example for each:

Behavior

Description

Code example

ConstrainExtentBehavior

Limit the map extent to an envelope. The user cannot navigate outside the envelope defined in the behavior.

<esri:Map x:Name="MyMap" Extent="-120,30,-60,60">
  . . . 
  <i:Interaction.Behaviors>
    <esriBehaviors:ConstrainExtentBehavior
      ConstrainedExtent="-120,30,-60,60"/> 
  </i:Interaction.Behaviors>
</esri:Map>

MaintainExtentBehavior

Maintain the current extent when the map is resized.

<esri:Map x:Name="MyMap">
  . . . 
  <i:Interaction.Behaviors>
    <esriBehaviors:MaintainExtentBehavior /> 
  </i:Interaction.Behaviors>
</esri:Map>

ShowCoordinatesBehavior

Show map coordinates next to the mouse cursor when hovering the mouse pointer over the map.

<esri:Map x:Name="MyMap">
  . . . 
  <i:Interaction.Behaviors>
    <esriBehaviors:ShowCoordinatesBehavior 
      FormatString="{}{0:0.00} , {1:0.00}"/>
  </i:Interaction.Behaviors>
</esri:Map>

Actions

An action is composed of a trigger and a target. The trigger is an event, such as the click event of a button. A target is where the results of the action are used or displayed. Actions included with the ArcGIS API for Silverlight will always target the map and its contents. Actions include adding graphics, navigating the map, and working with layers. The table below lists available actions, provides a description, and shows a code example for each:

Action

Description

Code example

ClearGraphicsAction

Clear all graphics in a graphics layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:ClearGraphicsAction 
        GraphicsLayerID="MyGraphicsLayer"
        TargetName="MyMap" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

MeasureAction

Show measure distance, radius, and area.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:MeasureAction 
        AreaUnit="SquareMiles"
        DisplayTotals="True"
        DistanceUnit="Miles"
        MapUnits="DecimalDegrees"
        MeasureMode="Polygon" 
        FillSymbol="{StaticResource DefaultFillSymbol}"
        TargetName="MyMap"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

PanToAction

Pan to a specified geometry.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:PanToAction
        TargetName="MyMap">
        <esri:PanToAction.Geometry>
          <esri:MapPoint X="-120" Y="43" />
        </esri:PanToAction.Geometry>
      </esri:PanToAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

RedlineAction

Draw graphics on the map and add them to a graphics layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:RedlineAction 
        Color="#FF32FF00"
        DrawMode="Freehand" 
        GraphicsLayerID="MyGraphicsLayer" 
        TargetName="MyMap"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

SpatialQueryAction

Draw geometry on the map to query features in a feature layer. Draw the results in a graphics layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:SpatialQueryAction
        DrawMode="Rectangle"
        LayerID="MyGraphicsLayer"
        Url="http://myserver/ArcGIS/rest/myservice/MapServer/2"
        Symbol="{StaticResource GraphicsLayerFillSymbol}"
        TargetName="MyMap" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

ToggleLayerAction

Toggle the visibility of a layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:ToggleLayerAction 
        LayerID="MyDynamicLayer"
        TargetName="MyMap"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

UpdateFeatureLayerAction

Refresh the contents of a feature layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:UpdateFeatureLayerAction
        FeatureLayerID="MyFeatureLayer" 
        TargetName="MyMap" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

ZoomToAction

Zoom to a specified geometry. If the geometry is a point, the map will pan.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:ZoomToAction
        TargetName="MyMap">
        <esri:ZoomToAction.Geometry>
          <esri:Envelope 
            XMin="-110" YMin="40" XMax="-100" YMax="50" />
        </esri:ZoomToAction.Geometry>
      </esri:ZoomToAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

ZoomToFullExtentAction

Zoom to the full extent of all layers.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:ZoomToFullExtentAction 
        TargetName="MyMap"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

ZoomToLayerAction

Zoom to the extent of a specified layer.

<Button>
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <esri:ZoomToLayerAction 
        LayerID="MyTileLayer" 
        TargetName="MyMap"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

6/23/2013