How to create a Query task

An example of XAML and .NET code (in this case C#) for a simple WPF application that includes a Query task working with local data is shown below. This application defines a Query task that uses a TextBox control for specifying the query and a Button for executing the task. Result features are displayed in a GraphicsLayer and specified attributes fields in a DataGrid. The rest of this document will walk you through how the Query task is defined in the example including a section on working with online data.

<Window x:Class="ArcGISWpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
        <Grid x:Name="LayoutRoot">        
        
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="ResultsSymbol" Color="Red"/>
        </Grid.Resources>
        
        <!-- Map Control -->
        <esri:Map x:Name="MyMap">  
            <esri:Map.Layers>
                <esri:ArcGISLocalDynamicMapServiceLayer Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/>
                <esri:GraphicsLayer ID="MyGraphicsLayer">                                      
                </esri:GraphicsLayer>
        </esri:Map.Layers>
        </esri:Map>
        <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250">
            <Rectangle Fill="Green" Stroke="Gray" RadiusX="10" RadiusY="10" Width="230" Height="55" />
            <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
            <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP2000>350000" />
            <Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
        </Canvas>

        <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
        Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
        <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Canvas.Left="0" Canvas.Top="0" 
        Width="190" Height="200" >
        </Rectangle> 
        <TextBlock x:Name="DataDisplayTitle" Text="Search Results" Foreground="White" FontSize="10" 
         Canvas.Left="10" Canvas.Top="4"/> 
        <DataGrid x:Name="QueryDetailsDataGrid" Width="170" Height="165" AutoGenerateColumns="False" 
         HeadersVisibility="Column" Background="White" IsReadOnly="True" Canvas.Left="10" Canvas.Top="25"
         HorizontalScrollBarVisibility="Hidden">                                            
        <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Attributes[AREANAME]}" Width="75" Header="City Name"/>
        <DataGridTextColumn Binding="{Binding Attributes[POP2000]}" Width="80" Header="Pop 2000"/>
        </DataGrid.Columns>
        </DataGrid>
       </Canvas>
    </Grid>
</Window>

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client.Tasks;

namespace ArcGISWpfApplication1
{
    public partial class MainWindow : Window
    {
       QueryTask queryTask;

        public MainWindow()
        {
            InitializeComponent();

            LocalMapService.GetServiceAsync(@"C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk", ms =>
            {
                 queryTask = new QueryTask();
                 queryTask.Url = ms.UrlMapService + "/0";
                 queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
                 queryTask.Failed += QueryTask_Failed;
            });
        }

        private void QueryButton_Click(object sender, RoutedEventArgs e)
        {
          Binding resultFeaturesBinding = new Binding("LastResult.Features");
          resultFeaturesBinding.Source = queryTask;
          QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);

          Query query = new Query();
          query.ReturnGeometry = true;
          query.OutFields.AddRange(new string[] { "AREANAME", "POP2000" });
          query.Where = QueryTextBox.Text;
          queryTask.ExecuteAsync(query);
        }

        private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Query failed: " + args.Error);
        }

        private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            if (args.FeatureSet.Features.Count > 0)
            {
                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.ClearGraphics();

                if (args.FeatureSet.Features.Count > 0)
                {
                    foreach (Graphic resultFeature in args.FeatureSet.Features)
                    {
                        ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
                            {
                                Geometry = resultFeature.Geometry,
                                Symbol = LayoutRoot.Resources["ResultsSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
                            };

                        graphicsLayer.Graphics.Add(graphic);
                        ResultsDisplay.Visibility = Visibility.Visible;
                    }
                }
                else
                {
                    MessageBox.Show("No features found");
                }
            }
        }
    }
}

The following steps assume you have created a WPF application with a map and a local layer as described in Creating a map. The XAML view of your application's main window (e.g. MainWindow.xaml) should be similar to the following:

<Window x:Class="ArcGISWpfApplication1.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
 Title="MainWindow">
	<Grid>

		<!-- MAP -->
		<esri:Map x:Name="MyMap">
  <esri:LocalArcGISDynamicMapServiceLayer Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/>
		</esri:Map>
	</Grid>
</Window>

Creating an input interface for the Query task

Since the QueryTask does not define the user interface this will need to be implemented to allow the user to execute the task. For this, the example includes a TextBox for defining the query and a Button to execute the task.

  1. In the XAML of the application's main page (e.g. MainWindow.xaml), define a Canvas to hold the task's input interface.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >      
    </Canvas>
    
  2. Specify a Rectangle to use as the background for the input interface.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray" RadiusX="10" RadiusY="10" Width="230" Height="55" />
    </Canvas>
    
  3. Add a TextBlock to inform the user how to use the task.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
      <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
    </Canvas>
    
  4. Define a TextBox for specifying the query.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
      <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
      <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" />
    </Canvas>
    
  5. Add a default query to the TextBox.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
      <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
      <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP2000>350000" />
    </Canvas>
    
  6. Add a Button to execute the query.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
      <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
      <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP2000>350000" />
      <Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" />
    </Canvas>
    
  7. Specify a handler for the Button's Click event. Later, you will implement this handler so that it executes the query.
    <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250" >
      <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Width="230" Height="55" />
      <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
      <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP2000>350000"/>
      <Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
    </Canvas>
    

Creating an output interface for the Query task

To display the results of the Query task, you need to specify an output interface. For displaying the geometry of results, you will define a GraphicsLayer in the Map element and a SimpleFillSymbol as a static resource. Then you will define a DataGrid for displaying results' attributes.

  1. Add a SimpleMarkerSymbol as a resource in your WPF application. Later, you will apply this symbol to the task's results in the window's code-behind.
    <Grid.Resources>
    	<esri:SimpleMarkerSymbol x:Key="ResultsSymbol" Color="Red"/>
    </Grid.Resources>
    
  2. Add a GraphicsLayer to the Map control XAML element. Note that the GraphicsLayer is specified below the map service layer in the XAML so that it is drawn above the map service layer at run time.
    <esri:Map x:Name="MyMap">
    	<esri:Map.Layers>
      <esri:LocalArcGISDynamicMapServiceLayer Path="C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk"/>
    		<esri:GraphicsLayer ID="MyGraphicsLayer">
    		</esri:GraphicsLayer>
    	</esri:Map.Layers>
    </esri:Map>
    
  3. Create a Canvas to hold the output interface. Specify the horizontal and vertical alignment to position the Canvas in the bottom right of the window. Specify the visibility as collapsed so it is not visible until later when you set the visibility in the code behind.
    <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
    Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
    </Canvas>
    
  4. Define a Rectangle to hold the DataGrid.
    <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
    Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
    <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Canvas.Left="0" Canvas.Top="0" 
    Width="190" Height="200" >
    </Rectangle>    
    </Canvas>
    
  5. Add a TextBlock to inform the user
    <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
    Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
    <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Canvas.Left="0" Canvas.Top="0" 
    Width="190" Height="200" >
    </Rectangle> 
    <TextBlock x:Name="DataDisplayTitle" Text="Search Results" Foreground="White" FontSize="10" 
    Canvas.Left="10" Canvas.Top="4"/> 
    </Canvas>
    
  6. Add a DataGrid and specify two DataGrid Columns to display the Query task results. Bind to the AREANAME attributes and the POP2001 fields of the Query task results to show the city names and the population density values returned from the Query task.
    <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
    Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
    <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Canvas.Left="0" Canvas.Top="0" 
    Width="190" Height="200" >
    </Rectangle> 
    <TextBlock x:Name="DataDisplayTitle" Text="Search Results" Foreground="White" FontSize="10" 
    Canvas.Left="10" Canvas.Top="4"/> 
    <DataGrid x:Name="QueryDetailsDataGrid" Width="170" Height="165" AutoGenerateColumns="False" 
    HeadersVisibility="Column" Background="White"IsReadOnly="True" Canvas.Left="10" Canvas.Top="25"
    HorizontalScrollBarVisibility="Hidden">                                            
    <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Attributes[AREANAME]}" Width="75" Header="City Name"/>
    <DataGridTextColumn Binding="{Binding Attributes[POP2000]}" Width="80" Header="Pop 2000"/>
    </DataGrid.Columns>
    </DataGrid>
    </Canvas>
    

Implementing the Query task's execution logic

Now that you've specified the Query task's user interface, you need to define its execution logic. This execution logic can be divided into three parts:

  • Task execution
  • Task results display
  • Execution error handling

You will implement these components in .NET code contained in the main window's code-behind. This code is linked to the XAML presentation layer by manipulating elements that you declared in XAML with "x:Name" or "ID" attributes and implementing methods that you declared in XAML as event handlers. The steps below assume that you are adding code to the Window class in the code-behind file for your WPF application's main page (e.g. MainWindow.xaml.cs). In this example, C# is used.

Executing the task

To execute a query task, you need to instantiate the task, specify the layer that will be queried, wire the task's event handlers, initialize the task's query parameters, and call the task's execution method. The steps below will show you how to do this in the code-behind of your application's main page (e.g. MainWindow.xaml.cs). The task is declared and initialized in the code-behind because tasks alone do not define any user interface, but rather encapsulate pieces of execution logic. In WPF, XAML is reserved for an application's presentation layer, while the code-behind is where business logic is implemented.

The code shown in these steps is written in C#.

  1. In the code-behind find the default constructor and implement code to start the LocalMapService. The querytask constructor accepts the LocalMapService URL with the layer specified. This example uses the first layer in the USCitiesStates sample data. Specify a handler for the task's ExecuteCompleted event. The method specified will be called when the Query task is done executing. You will implement the handler in the "Displaying results" section. Specify a handler for the task's Failed event, which fires when there is a problem executing the query. You will define the handler in the "Handling execution errors" section below.
    QueryTask queryTask;
    
    public MainWindow()
    {
        InitializeComponent();
       
        LocalMapService.GetServiceAsync(@"C:\Program Files (x86)\ArcGIS SDKs\WPF10.2.5\sdk\samples\data\mpks\USCitiesStates.mpk", ms =>
        {
            queryTask = new QueryTask();
            queryTask.Url = ms.UrlMapService + "/0";
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;
        });
    }
    
  2. In the code-behind class of your application's main window, implement a handler for the QueryButton control's click event. Recall that you declared this handler when you defined the QueryButton control in the page's XAML.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
           
    }
    
  3. To bind the DataGrid to the query results, declare a Binding object and instatiate it with the Binding path. Set the queryTask as the binding source and attach the specified binding to the DataGrid.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    }
    
  4. Declare a Query object and instantiate it. The Query object is used to define the execution parameters for Query tasks.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    	Query query = new Query();
    }
    
  5. Since you will draw the query results on the map, specify that the query return geometry with the results.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    }
    
  6. Define the fields to return with the query results. Here you will specify that the query return the city name and population density fields. Note that only the fields required by your application should be returned so that network traffic is minimized.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "AREANAME", "POP2000" });
    }
    
  7. Specify the where clause for the query as the text contained in the QueryTextBox control. The where clause defines the conditions that features must satisfy to be returned in the query results.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "AREANAME", "POP2000" });
    	query.Where = QueryTextBox.Text;
    }
    
  8. Execute the query task.
    private void QueryButton_Click(object sender, RoutedEventArgs e)
    {
    	
    Binding resultFeaturesBinding = new Binding("LastResult.Features");
    resultFeaturesBinding.Source = queryTask;
    QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
    
    	Query query = new Query();
    	query.ReturnGeometry = true;
    	query.OutFields.AddRange(new string[] { "AREANAME", "POP2000" });
    	query.Where = QueryTextBox.Text;
    
    	queryTask.ExecuteAsync(query);
     
    }
    

Displaying results

  1. Declare a handler for the Query task's ExecuteCompleted event. This handler will be invoked when a query is complete. A FeatureSet containing the features that satisfy the query is passed to the handler's args parameter.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    }
    
  2. Get a reference to the results GraphicsLayer and clear any previously added graphics from it.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    }
    
  3. Check whether any results satisfying the query were found.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    	}
    	else
    	{
    	}
    }
    
  4. If results were found, loop through them. Create a graphic for each feature by applying the feature's geometry and the results marker symbol you declared in the window's XAML to each feature. Then add it to the results GraphicsLayer. Make the DataGrid visible.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    		foreach (Graphic resultFeature in args.FeatureSet.Features)
    		{
    			ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
       {
       Geometry = resultFeature.Geometry,
       Symbol = LayoutRoot.Resources["ResultsSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
     };
    			graphicsLayer.Graphics.Add(graphic);
       ResultsDisplay.Visibility = Visibility.Visible;
    		}
    	}
    	else
    	{
    	}
    }
    
  5. If no features satisfied the query, notify the user with a MessageBox.
    private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
    {
    	GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
    	graphicsLayer.ClearGraphics();
    
    	if (args.FeatureSet.Features.Count > 0)
    	{
    		foreach (Graphic resultFeature in args.FeatureSet.Features)
    		{
    				ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
       {
       Geometry = resultFeature.Geometry,
       Symbol = LayoutRoot.Resources["DefaultPictureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
     };
    			graphicsLayer.Graphics.Add(graphic);
       ResultsDisplay.Visibility = Visibility.Visible;
    		}
    	}
    	else
    	{
    		MessageBox.Show("No features found");
    	}
    }
    

Handling execution errors

  1. Declare a handler for the Query task's Failed event. This handler will be invoked if there is a problem with executing a query.
    private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
    }
    
  2. Notify the user of the problem with a MessageBox.
    private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
    {
    	MessageBox.Show("Query failed: " + args.Error);
    }
    

The Query task with online data

The Query task can also be performed on online data. To use the sample with online data modify the URL inputted into the Query task constructor to be a URL to a feature layer in an ArcGIS for Server map service or a table in a SQL Server database served by the MapIt Spatial Data Service. To find a URL, you can use the ArcGIS Services Directory or the MapIt Spatial Data Services Directory. The example below uses the states layer in the ArcGIS for Server.

Modify the Query task outfields range to match the attribute fields of the online service.

private void QueryButton_Click(object sender, RoutedEventArgs e)
        {
          QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/
          services/" + "Demographics/ESRI_Census_USA/MapServer/5");         
          queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
          queryTask.Failed += QueryTask_Failed;
        
          Binding resultFeaturesBinding = new Binding("LastResult.Features");
          resultFeaturesBinding.Source = queryTask;
          QueryDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);

          Query query = new Query();
          query.ReturnGeometry = true;
         	query.OutFields.AddRange(new string[] { "STATE_NAME", "POP07_SQMI" });
          query.Where = QueryTextBox.Text;
          queryTask.ExecuteAsync(query);
      
        }

To display an online service in the Map Control the local service will need to be replaced by an online service.

<Window x:Class="ArcGISWpfApplication1.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
 Title="MainWindow">
	<Grid>

		<!-- MAP -->
		<esri:Map x:Name="MyMap">
	<esri:Map.Layers>
				<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>	
			</esri:Map.Layers>
		</esri:Map>
	</Grid>
</Window>

To bind to the DataGrid to display the results from the online service, the Binding attributes will have to be modified accordingly in the XAML view. The default query textbox value will need to be modified to make the query relevant to the online service.

<Window x:Class="ArcGISWpfApplication1.MainWindow"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
 Title="MainWindow">
	<Grid>

	    <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="ResultsSymbol" Color="Red"/>
        </Grid.Resources>
        
        <!-- Map Control -->
        <esri:Map x:Name="MyMap">  
            <esri:Map.Layers>
             		<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" 
					Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
        </esri:Map.Layers>
        </esri:Map>
        <Canvas HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,15,7,0" Width="250">
            <Rectangle Fill="Green" Stroke="Gray" RadiusX="10" RadiusY="10" Width="230" Height="55" />
            <TextBlock Text="Type a query and click Execute" Foreground="White" FontSize="10" Margin="10,5,0,0" />
            <TextBox x:Name="QueryTextBox" Width="150" Margin="15,22,0,0" Text="POP07_SQMI > 500" />
            <Button x:Name="QueryButton" Content="Execute" Margin="168,23,0,0" Click="QueryButton_Click" />
        </Canvas>

        <Canvas x:Name="ResultsDisplay" HorizontalAlignment="Right" VerticalAlignment="Bottom" 
        Width="200" Height="200" Margin="0,0,15,15" Visibility="Collapsed" >
        <Rectangle Fill="Green" Stroke="Gray"  RadiusX="10" RadiusY="10" Canvas.Left="0" Canvas.Top="0" 
        Width="190" Height="200" >
        </Rectangle> 
        <TextBlock x:Name="DataDisplayTitle" Text="Search Results" Foreground="White" FontSize="10" 
         Canvas.Left="10" Canvas.Top="4"/> 
        <DataGrid x:Name="QueryDetailsDataGrid" Width="170" Height="165" AutoGenerateColumns="False" 
         HeadersVisibility="Column" Background="White"IsReadOnly="True" Canvas.Left="10" Canvas.Top="25"
         HorizontalScrollBarVisibility="Hidden">                                            
        <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Attributes[STATE_NAME]}" Width="75" Header="State Name"/>
        <DataGridTextColumn Binding="{Binding Attributes[POP07_SQMI]}" Width="80" Header="Population"/>
        </DataGrid.Columns>
        </DataGrid>
       </Canvas>
    </Grid>
</Window>

1/27/2015