ArcGIS Runtime SDK for WPF - Library Reference
LegendItemTemplate Property
See Also  Example
ESRI.ArcGIS.Client.Toolkit Namespace > Legend Class : LegendItemTemplate Property

Gets or sets the DataTemplate that controls how the Legend Control will display information about individual LegendItems in a Layer. It is used at the lowest leaf level in the Legend hierachy and most generally contains an image and a label.

Syntax

Visual Basic (Declaration) 
Public Property LegendItemTemplate As DataTemplate
C# 
public DataTemplate LegendItemTemplate {get; set;}

Remarks

This DataTemplate controls what is viewed in the Legend for the lowest level of information about a particular Layer. It presents each LegendItem via its image (LegendItemViewModel.ImageSource) and label description (LegendItemViewModel.Label). No ToolTip information is provided for an individual LegendItem by default.

The objects that have Binding occur in the LegendItemTemplate are implied to be the Properties of the LegendItemViewModel Class.

At the Legend.LegendItemTemplate level, the customization options become more limited due to the map service information being passed back from the ArcGIS Server REST end point. The LegendItemViewModel class contains the most atomic level of information about a particular LegendItem. Turning on/off individual LegendItems or changing their opacity is not possible on any Layer type. It is possible to get creative and perform TOC style user interactions at the Legend.LegendItemtemplate level by discovering the parent objects of individual LegendItems; see the code example in the this document for one such customization.

It should be noted that it is possible to customize one or more of the Legend Control DataTemplates at the same time. There are established default values for each DataTemplate, so setting one will not necessarily override the others that have been set by default. Significant testing should be done on all of the Layers in the customized application to ensure that the desired behavior has been achieved. Some Layers have different behavior in rendering items in the Legend and should be tested thoroughly.

The following screen shot demonstrates which part of the Legend Control corresponds to the three DataTemplates. The Layer (ID = "United States") that is being displayed is an ArcGISDynamicMapServiceLayer with three sub-Layers (ushigh, states, and counties). The information found in the ArcGIS Services Directory about the ArcGISDynamicMapServiceLayer corresponds to what is shown in the Map and Legend Controls.

Using the ArcGIS Services Directory to view information about an ArcGISDynamicMapServiceLayer and how that corresponds to what is displayed in the Map and Legend Control. The DataTemplate parts of the Legend Control are specified.

TIP: It is typically necessary for developers to specify the Layer.ID name for Layers in the Map Control. This can be done in XAML or code-behind. If a Layer.ID value is not specified, then a default Layer.ID value is provided based upon the URL of the ArcGIS Server map service.

Example

How to use:

Click the 'Apply a custom LegendItemTemplate' button to change the default Legend into an interactive Table of Contents (TOC). This example shows how to apply a custom DataTemplate for the Legend.LegendItemTemplate. Move the cursor over an Image of any LegendItem and the features that correspond to that LegendItem with be selected (in blue) in the Map. When the cursor is moved off of the Image in the LegendItem the selection will clear.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Applying a custom LegendItemTemplate to the Legend Control.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
    
  <!-- 
  NOTE: Don't forget to add the following xml namespace definition to the XAML file:
  xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
  -->
  
  <!-- Define a Resources section for use later in the XAML and code-behind -->
  <Grid.Resources>
          
    <!--
    Construct a DataTemplate that will be used to override the default behavior of the Legend.LegendItemTemplate.
    Important things to note are:
    ~ When the user moves the cursor over the Image of a LegendItem, the MouseEnter event will fire and custom coding 
    will occur in the code-behind that performs a Query to display selected graphic that correspond to the LegendItem
    in the Map.
    ~ When the user moves the cursor off the Image of the LegendItem, the GraphicsLayer will be cleared.
    ~ The Image.Tag Property is bound to the LegendItemViewModel.Tag Property. The LegendItemViewModel.Tag gets 
    populated as part of the Legend.Refreshed event. The purpose of populating the LegendItemViewModel.Tag is to 
    pass the parent object (i.e. the Layer/sub-Layer) associated with a specific LegendItem so that a Query can 
    occur correctly.
    ~ The objects that have Binding occur in the DataTemplate are implied to be the Properties of the 
    ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel Class.
    -->
    <DataTemplate x:Key="My_LegendItemTemplate">
      <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ImageSource}" MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" Tag="{Binding Tag}"/>
        <sdk:Label Content="{Binding Label}"  FontSize="14" FontWeight="Bold"/>
      </StackPanel>
    </DataTemplate>
    
    <!-- Construct some default SimpleFillSymbol and SimpleLineSymbol objects for use in the GraphicsLayer. -->
    <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#500000FF" BorderBrush="Blue" BorderThickness="1" />
    <esri:SimpleLineSymbol x:Key="DefaultLineSymbol"  Color="Blue" />
    
  </Grid.Resources>
  
  <!-- Add a Map Control. -->
  <esri:Map Background="White" HorizontalAlignment="Left" Margin="12,188,0,0" Name="Map1" 
            VerticalAlignment="Top" Height="400" Width="400" >
  
    <!-- Add an ArcGISDynamicMapServiceLayer. It will have three sub-Layers: counties, states, and ushigh. -->
    <esri:ArcGISDynamicMapServiceLayer  Opacity="0.6" ID="MyArcGISDynamicMapServiceLayer"
           Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer"/>
    
    <!-- Add an empty GraphicsLayer used for rendering selected features that correspond to LegendItems. -->
    <esri:GraphicsLayer ID="MyGraphicsLayer" />
    
  </esri:Map>
  
  <!-- 
  Add a Legend. It is bound to the Map Control. The LegendItemTemplate will get overridden in the code-behind 
  using the DataTemplate defined in the Resources section of the XAML.
  -->
  <esri:Legend HorizontalAlignment="Left" Margin="418,188,0,0" Name="Legend1" 
               VerticalAlignment="Top" Width="350" Height="400" Background="#FF2180D4"
               Map="{Binding ElementName=Map1}" LayerItemsMode="Tree" ShowOnlyVisibleLayers="False"/>
  
  <!-- Add a button to apply the custom DataTemplate and thus allowing the other customized functionality. -->
  <Button Content="Apply a custom LegendItemTemplate" Name="Button1"
          Height="23" HorizontalAlignment="Left" Margin="12,159,0,0" 
          VerticalAlignment="Top" Width="756" 
          Click="Button1_Click"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="95" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="756" 
         TextWrapping="Wrap" Margin="12,12,0,0" 
         Text="Click the 'Apply a custom LegendItemTemplate' button to change the default Legend into an interactive
             Table of Contents (TOC). This example shows how to apply a custom DataTemplate for the 
             Legend.LegendItemTemplate. Move the cursor over an Image of any LegendItem and the features that
             correspond to that LegendItem with be selected (in blue) in the Map. When the cursor is moved off
             of the Image in the LegendItem the selection will clear." />
</Grid>
C#Copy Code
// NOTE: Don't forget to insert the following event handler wireups at the end of the 'InitializeComponent' 
// method for forms, 'Page_Init' for web pages, or into a constructor for other classes:
Legend1.Refreshed += new ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventHandler(Legend1_Refreshed);
            
private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Apply the custom DataTemplate that was defined in XAML to the Legend's DataTemplate.
  DataTemplate myDataTemplate = LayoutRoot.Resources["My_LegendItemTemplate"];
  Legend1.LegendItemTemplate = myDataTemplate;
  
  // Only show the ArcGISDynamicMapServiceLayer in the Legend Control. Do not display the GraphicsLayer.
  Legend1.LayerIDs = new string[] {"MyArcGISDynamicMapServiceLayer"};
}
            
private void Legend1_Refreshed(object sender, ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs e)
{
  // The purpose of the code in this function is to associate (1) the Layer/sub-Layer name and (2) the Label associated 
  // the image of a LegendItem to the LegendItemViewModel.Tag Property. This will enable the correct Query selection
  // to occur when the user moves the cursor over the LegendItem image in the Legend Control.
  
  // Get the Legend.
  ESRI.ArcGIS.Client.Toolkit.Legend theLegend = (ESRI.ArcGIS.Client.Toolkit.Legend)sender;
  
  // Get the LayerItems of the Legend Control.
  Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel> theObservableCollection = theLegend.LayerItems;
  
  // Loop through the ObservableCollection<LayerItemViewModel> objects for each Layer. There are two Layers in this 
  // sample (an ArcGISDynamicMapServiceLayer and an empty GraphicsLayer).
  foreach (ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel theLayerItemViewModel in theObservableCollection)
  {
    // Get the LayerItems for each Layer.
    Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel> theObservableCollection2 = theLayerItemViewModel.LayerItems;
    
    // Make sure that we have a valid ObservableCollection<LayerItemViewModel> object before continuing. The GraphicsLayer 
    // does not have any LayerItems so it will return a Nothing/null. 
    if (theObservableCollection2 != null)
    {
      // Loop through the ObservableCollection<LayerItemViewModel> objects for each sub-Layer. For the 
      // ArcGISDynamicMapServiceLayer there will be three sub-Layers (i.e. counties, states, and ushigh).
      foreach (ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel theLayerItemViewModel2 in theObservableCollection2)
      {
        // Get the LegendItems for each sub-Layer.
        Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel> theObservableCollection3 = theLayerItemViewModel2.LegendItems;
        
        // Loop through the ObservableCollection<LegendItemViewModel> objects for each LegendItem of the sub-Layer.
        // The states and ushigh sub-layers only have one LegendItem (i.e. a symbol with a label). The counties sub-Layer
        // however has numerous LegendItems.
        foreach (ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel theLegendItemViewModel in theObservableCollection3)
        {
          // Create a List object to hold:
          // (1) the name of the sub-Layer 
          // (2) the string label associated with the image of the LegendItem
          //
          // The reason for obtaining this information is there is NOT a 'Parent' object property of the LegendItemViewModel 
          // which makes it a little tricky to figure out what Layer/sub-Layer a particular LegendItem belongs to. Hence 
          // it requires some more complex coding to determine this information and we will store it in the .Tag property
          // of each individual LegendItem for use later in the application.
          List<string> theList = new List<string>();
          theList.Add(theLayerItemViewModel2.Label); // The Layer/sub-Layer name (i.e. "counties", "states", or "ushigh")
          theList.Add(theLegendItemViewModel.Label); // The Label of the LegendItem.
          theLegendItemViewModel.Tag = theList;
        }
      }
    }
  }
}
            
private void Image_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
  // This function will perform a 'selection' type of operation each time the user moves the cursor over the image
  // associated with a LegendItem in the Legend Control. The correct type of Query syntax will be obtained from the
  // Image.Tag Property which contains a List with two strings. The first string gives the name of the Layer/sub-Layer
  // and the second string is the Label of the particular LegendItem associated with the Image.
  
  // Create and initial QueryTask object. It will be fully instantiated later in the code.
  ESRI.ArcGIS.Client.Tasks.QueryTask theQueryTask = null;
  
  // Create a new Query and set its initial Properties.
  ESRI.ArcGIS.Client.Tasks.Query theQuery = new ESRI.ArcGIS.Client.Tasks.Query();
  theQuery.OutSpatialReference = Map1.SpatialReference;
  theQuery.OutFields.Add("*");
  theQuery.ReturnGeometry = true;
  
  // Get the information necessary to construct the Query syntax from the Image.Tag Property.
  List<string> theList = sender.Tag;
  string theLayerName = theList[0];
  string theLegendItem = theList[1];
  
  // Determine which sub-Layer of the ArcGISDynamicMapServiceLayer we will be constructing the Query for and
  // use the appropriate syntax to construct the QueryTask. NOTE: this example is very much hard-coded for
  // specific sample data; you will need to adjust the code according to your sample data.
  if (theLayerName == "counties")
  {
    theQuery.Where = "STATE_NAME = '" + theLegendItem + "'";
    theQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/2");
  }
  else if (theLayerName == "states")
  {
    theQuery.Where = "1 = 1";
    theQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1");
  }
  else if (theLayerName == "ushigh")
  {
    theQuery.Where = "1 = 1";
    theQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/0");
  }
  
  // Add handlers for the Asynchronous processing.
  theQueryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
  theQueryTask.Failed += QueryTask_Failed;
  
  // Execute the QueryTask.
  theQueryTask.ExecuteAsync(theQuery);
}
            
private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
  // This Asynchronous function will occur when the QueryTask has completed executing on the web server. It's 
  // purpose is to add any features returned from the QueryTask and put them in the GraphicsLayer symbolized
  // properly.
  // Remember that depending on the ArcGIS Server settings only the first 500 or 1000 features will be returned. 
  // In ArcGIS Server 9.3.1 and prior, the default maximum is 500 records returned per Query. In ArcGIS Server 10 
  // the default is 1000. This setting is configurable per map service using ArcCatalog or ArcGIS Server Manager 
  // (on the Parameters tab). 
  
  // Get the FeatureSet from the QueryTask operation.
  ESRI.ArcGIS.Client.Tasks.FeatureSet theFeatureSet = args.FeatureSet;
  
  // Obtain the GraphicsLayer that was defined in XAML and clear out and Graphics that may have previously been there.
  ESRI.ArcGIS.Client.GraphicsLayer theGraphicsLayer = Map1.Layers["MyGraphicsLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
  theGraphicsLayer.ClearGraphics();
  
  // If we have 1 or more features returned from the QueryTask proceed.
  if (theFeatureSet != null && theFeatureSet.Features.Count > 0)
  {
    // Loop through each feature (i.e. a Graphic) in the FeatureSet
    foreach (ESRI.ArcGIS.Client.Graphic theGraphic in theFeatureSet)
    {
      // Depending the GeometryType of the Graphic set the appropriate Symbol from what was
      // defined in the LayoutRoot.Resources of XAML.
      if (theFeatureSet.GeometryType == ESRI.ArcGIS.Client.Tasks.GeometryType.Polygon)
      {
        theGraphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
      }
      else if (theFeatureSet.GeometryType == ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline)
      {
        theGraphic.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
      }
      
      // Add the Graphic from the FeatureSet to the GraphicsLayer.
      theGraphicsLayer.Graphics.Add(theGraphic);
    }
  }
}
            
private void QueryTask_Failed(object sender, ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs args)
{
  // Display some error help if there is something wrong.
  MessageBox.Show("Query failed: " + args.Error.ToString());
}
            
private void Image_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
  // If the user moves the cursor off of the Image in the LegendItem, clear the selected features in the GraphicsLayer.
  ESRI.ArcGIS.Client.GraphicsLayer graphicsLayer = Map1.Layers["MyGraphicsLayer"] as ESRI.ArcGIS.Client.GraphicsLayer;
  graphicsLayer.ClearGraphics();
}
VB.NETCopy Code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  
  ' Apply the custom DataTemplate that was defined in XAML to the Legend's DataTemplate.
  Dim myDataTemplate As DataTemplate = LayoutRoot.Resources("My_LegendItemTemplate")
  Legend1.LegendItemTemplate = myDataTemplate
  
  ' Only show the ArcGISDynamicMapServiceLayer in the Legend Control. Do not display the GraphicsLayer.
  Legend1.LayerIDs = New String() {"MyArcGISDynamicMapServiceLayer"}
  
End Sub
  
Private Sub Legend1_Refreshed(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Toolkit.Legend.RefreshedEventArgs) Handles Legend1.Refreshed
  
  ' The purpose of the code in this function is to associate (1) the Layer/sub-Layer name and (2) the Label associated 
  ' the image of a LegendItem to the LegendItemViewModel.Tag Property. This will enable the correct Query selection
  ' to occur when the user moves the cursor over the LegendItem image in the Legend Control.
  
  ' Get the Legend.
  Dim theLegend As ESRI.ArcGIS.Client.Toolkit.Legend = sender
  
  ' Get the LayerItems of the Legend Control.
  Dim theObservableCollection As Collections.ObjectModel.ObservableCollection(Of ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel) = theLegend.LayerItems
  
  ' Loop through the ObservableCollection(Of LayerItemViewModel) objects for each Layer. There are two Layers in this 
  ' sample (an ArcGISDynamicMapServiceLayer and an empty GraphicsLayer).
  For Each theLayerItemViewModel As ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel In theObservableCollection
    
    ' Get the LayerItems for each Layer.
    Dim theObservableCollection2 As Collections.ObjectModel.ObservableCollection(Of ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel) = theLayerItemViewModel.LayerItems
        
    ' Make sure that we have a valid ObservableCollection(Of LayerItemViewModel) object before continuing. The GraphicsLayer 
    ' does not have any LayerItems so it will return a Nothing/null. 
    If theObservableCollection2 IsNot Nothing Then
          
      ' Loop through the ObservableCollection(Of LayerItemViewModel) objects for each sub-Layer. For the 
      ' ArcGISDynamicMapServiceLayer there will be three sub-Layers (i.e. counties, states, and ushigh).
      For Each theLayerItemViewModel2 As ESRI.ArcGIS.Client.Toolkit.Primitives.LayerItemViewModel In theObservableCollection2
        
        ' Get the LegendItems for each sub-Layer.
        Dim theObservableCollection3 As Collections.ObjectModel.ObservableCollection(Of ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel) = theLayerItemViewModel2.LegendItems
        
        ' Loop through the ObservableCollection(Of LegendItemViewModel) objects for each LegendItem of the sub-Layer.
        ' The states and ushigh sub-layers only have one LegendItem (i.e. a symbol with a label). The counties sub-Layer
        ' however has numerous LegendItems.
        For Each theLegendItemViewModel As ESRI.ArcGIS.Client.Toolkit.Primitives.LegendItemViewModel In theObservableCollection3
          
          ' Create a List object to hold:
          ' (1) the name of the sub-Layer 
          ' (2) the string label associated with the image of the LegendItem
          '
          ' The reason for obtaining this information is there is NOT a 'Parent' object property of the LegendItemViewModel 
          ' which makes it a little tricky to figure out what Layer/sub-Layer a particular LegendItem belongs to. Hence 
          ' it requires some more complex coding to determine this information and we will store it in the .Tag property
          ' of each individual LegendItem for use later in the application.
          Dim theList As New List(Of String)
          theList.Add(theLayerItemViewModel2.Label) ' The Layer/sub-Layer name (i.e. "counties", "states", or "ushigh")
          theList.Add(theLegendItemViewModel.Label) ' The Label of the LegendItem.
          theLegendItemViewModel.Tag = theList
        Next
      Next
    End If
  Next
  
End Sub
            
Private Sub Image_MouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
  
  ' This function will perform a 'selection' type of operation each time the user moves the cursor over the image
  ' associated with a LegendItem in the Legend Control. The correct type of Query syntax will be obtained from the
  ' Image.Tag Property which contains a List with two strings. The first string gives the name of the Layer/sub-Layer
  ' and the second string is the Label of the particular LegendItem associated with the Image.
  
  ' Create and initial QueryTask object. It will be fully instantiated later in the code.
  Dim theQueryTask As ESRI.ArcGIS.Client.Tasks.QueryTask = Nothing
  
  ' Create a new Query and set its initial Properties.
  Dim theQuery As New ESRI.ArcGIS.Client.Tasks.Query()
  theQuery.OutSpatialReference = Map1.SpatialReference
  theQuery.OutFields.Add("*")
  theQuery.ReturnGeometry = True
  
  ' Get the information necessary to construct the Query syntax from the Image.Tag Property.
  Dim theList As List(Of String) = sender.Tag
  Dim theLayerName As String = theList.Item(0)
  Dim theLegendItem As String = theList.Item(1)
  
  ' Determine which sub-Layer of the ArcGISDynamicMapServiceLayer we will be constructing the Query for and
  ' use the appropriate syntax to construct the QueryTask. NOTE: this example is very much hard-coded for
  ' specific sample data; you will need to adjust the code according to your sample data.
  If theLayerName = "counties" Then
    theQuery.Where = "STATE_NAME = '" + theLegendItem + "'"
    theQueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/2")
  ElseIf theLayerName = "states" Then
    theQuery.Where = "1 = 1"
    theQueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1")
  ElseIf theLayerName = "ushigh" Then
    theQuery.Where = "1 = 1"
    theQueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/0")
  End If
  
  ' Add handlers for the Asynchronous processing.
  AddHandler theQueryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
  AddHandler theQueryTask.Failed, AddressOf QueryTask_Failed
            
  ' Execute the QueryTask.
  theQueryTask.ExecuteAsync(theQuery)
  
End Sub
            
Private Sub QueryTask_ExecuteCompleted(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
  
  ' This Asynchronous function will occur when the QueryTask has completed executing on the web server. It's 
  ' purpose is to add any features returned from the QueryTask and put them in the GraphicsLayer symbolized
  ' properly.
  ' Remember that depending on the ArcGIS Server settings only the first 500 or 1000 features will be returned. 
  ' In ArcGIS Server 9.3.1 and prior, the default maximum is 500 records returned per Query. In ArcGIS Server 10 
  ' the default is 1000. This setting is configurable per map service using ArcCatalog or ArcGIS Server Manager 
  ' (on the Parameters tab). 
  
  ' Get the FeatureSet from the QueryTask operation.
  Dim theFeatureSet As ESRI.ArcGIS.Client.Tasks.FeatureSet = args.FeatureSet
  
  ' Obtain the GraphicsLayer that was defined in XAML and clear out and Graphics that may have previously been there.
  Dim theGraphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = TryCast(Map1.Layers("MyGraphicsLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
  theGraphicsLayer.ClearGraphics()
  
  ' If we have 1 or more features returned from the QueryTask proceed.
  If theFeatureSet IsNot Nothing AndAlso theFeatureSet.Features.Count > 0 Then
    
    ' Loop through each feature (i.e. a Graphic) in the FeatureSet
    For Each theGraphic As ESRI.ArcGIS.Client.Graphic In theFeatureSet
      
      ' Depending the GeometryType of the Graphic set the appropriate Symbol from what was
      ' defined in the LayoutRoot.Resources of XAML.
      If theFeatureSet.GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polygon Then
        theGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
      ElseIf theFeatureSet.GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Polyline Then
        theGraphic.Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
      End If
      
      ' Add the Graphic from the FeatureSet to the GraphicsLayer.
      theGraphicsLayer.Graphics.Add(theGraphic)
      
    Next
    
  End If
  
End Sub
            
Private Sub QueryTask_Failed(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs)
  
  ' Display some error help if there is something wrong.
  MessageBox.Show("Query failed: " + args.Error.ToString())
  
End Sub
  
Private Sub Image_MouseLeave(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
  
  ' If the user moves the cursor off of the Image in the LegendItem, clear the selected features in the GraphicsLayer.
  Dim graphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = TryCast(Map1.Layers("MyGraphicsLayer"), ESRI.ArcGIS.Client.GraphicsLayer)
  graphicsLayer.ClearGraphics()
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7, Windows 8

See Also

© ESRI, Inc. All Rights Reserved.