This sample demonstrates performing an attribute query, adding the
results to the map and a DataGrid, and zooming to the result. To
use the sample, select a state from the drop-down menu in the upper
right corner of the application.
In the sample's code-behind, a
QueryTask is used to query a states map service layer based on the
name selected in the drop-down menu. The result is then added as a
Graphic to the map and displayed in the DataGrid.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.AttributeQuery "
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 " Background = " White " >
< Grid.Resources >
< esri : SimpleFillSymbol x:Key = " DefaultFillSymbol " Fill = " #500000FF " BorderBrush = " Blue " BorderThickness = " 1 " />
< LinearGradientBrush x:Key = " PanelGradient " EndPoint = " 0.5,1 " StartPoint = " 0.5,0 " >
< LinearGradientBrush.RelativeTransform >
< TransformGroup >
< ScaleTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< SkewTransform CenterY = " 0.5 " CenterX = " 0.5 " />
< RotateTransform Angle = " 176 " CenterY = " 0.5 " CenterX = " 0.5 " />
< TranslateTransform />
</ TransformGroup >
</ LinearGradientBrush.RelativeTransform >
< GradientStop Color = " #FF145787 " Offset = " 0.16 " />
< GradientStop Color = " #FF3D7FAC " Offset = " 0.502 " />
< GradientStop Color = " #FF88C5EF " Offset = " 0.984 " />
</ LinearGradientBrush >
</ Grid.Resources >
< esri : Map x:Name = " MyMap " Extent = " -15000000,2000000,-7000000,8000000 " WrapAround = " True " >
< esri : ArcGISTiledMapServiceLayer ID = " StreetMapLayer "
Url = " http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer " />
< esri : GraphicsLayer ID = " MyGraphicsLayer " />
</ esri : Map >
< StackPanel Orientation = " Vertical " Margin = " 10 " HorizontalAlignment = " Right " VerticalAlignment = " Top " >
< Grid x:Name = " QueryGrid " HorizontalAlignment = " Right " VerticalAlignment = " Top " Margin = " 0,7,7,0 " >
< Rectangle Fill = " {StaticResource PanelGradient} " Stroke = " Gray " RadiusX = " 10 " RadiusY = " 10 " Margin = " 0,0,0,5 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< TextBlock x:Name = " DataDisplayTitleBottom " Text = " Query a layer " Foreground = " White " FontSize = " 12 "
Margin = " 10,3,0,5 " />
< StackPanel Orientation = " Vertical " Margin = " 15 " HorizontalAlignment = " Center " VerticalAlignment = " Top " >
< ComboBox x:Name = " QueryComboBox " MinWidth = " 150 " SelectionChanged = " QueryComboBox_SelectionChanged "
Margin = " 5,10,5,5 " >
</ ComboBox >
< ScrollViewer x:Name = " DataGridScrollViewer " HorizontalScrollBarVisibility = " Hidden " VerticalScrollBarVisibility = " Auto "
Width = " 230 " MaxHeight = " 340 " Visibility = " Collapsed " >
< DataGrid x:Name = " QueryDetailsDataGrid " AutoGenerateColumns = " False " HeadersVisibility = " None "
Background = " White " >
< DataGrid.Columns >
< DataGridTextColumn Width = " 95 " Binding = " {Binding Path=Key} " FontWeight = " Bold " IsReadOnly = " True " />
< DataGridTextColumn Width = " 115 " Binding = " {Binding Path=Value} " IsReadOnly = " True " />
</ DataGrid.Columns >
</ DataGrid >
</ ScrollViewer >
</ StackPanel >
</ Grid >
</ StackPanel >
</ Grid >
</ UserControl >
using System;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
public partial class AttributeQuery : UserControl
{
public AttributeQuery()
{
InitializeComponent();
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;
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
// Specify fields to return from initial query
query.OutFields.AddRange(new string [] { "STATE_NAME" });
// This query will just populate the drop-down, so no need to return geometry
query.ReturnGeometry = false ;
// Return all features
query.Where = "1=1" ;
queryTask.ExecuteAsync(query, "initial" );
}
private void QueryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (QueryComboBox.SelectedItem.ToString().Contains("Select..." ))
return ;
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;
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
query.OutFields.Add("*" );
query.ReturnGeometry = true ;
query.Text = QueryComboBox.SelectedItem.ToString();
query.OutSpatialReference = MyMap.SpatialReference;
queryTask.ExecuteAsync(query);
}
private void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
{
FeatureSet featureSet = args.FeatureSet;
// If initial query to populate states combobox
if ((args.UserState as string ) == "initial" )
{
// Just show on initial load
QueryComboBox.Items.Add("Select..." );
foreach (Graphic graphic in args.FeatureSet.Features)
{
QueryComboBox.Items.Add(graphic.Attributes["STATE_NAME" ].ToString());
}
QueryComboBox.SelectedIndex = 0;
return ;
}
// Remove the first entry if "Select..."
if (QueryComboBox.Items[0].ToString().Contains("Select..." ))
QueryComboBox.Items.RemoveAt(0);
// If an item has been selected
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer" ] as GraphicsLayer;
graphicsLayer.ClearGraphics();
if (featureSet != null && featureSet.Features.Count > 0)
{
// Show selected feature attributes in DataGrid
Graphic selectedFeature = featureSet.Features[0];
QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes;
// Hightlight selected feature
selectedFeature.Symbol = LayoutRoot.Resources["DefaultFillSymbol" ] as ESRI.ArcGIS.Client.Symbols.Symbol;
graphicsLayer.Graphics.Add(selectedFeature);
// Zoom to selected feature (define expand percentage)
ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = selectedFeature.Geometry.Extent;
double expandPercentage = 30;
double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 100);
double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 100);
ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
selectedFeatureExtent.XMin - (widthExpand / 2),
selectedFeatureExtent.YMin - (heightExpand / 2),
selectedFeatureExtent.XMax + (widthExpand / 2),
selectedFeatureExtent.YMax + (heightExpand / 2));
MyMap.ZoomTo(displayExtent);
// If DataGrid not visible (initial load), show it
if (DataGridScrollViewer.Visibility == Visibility.Collapsed)
{
DataGridScrollViewer.Visibility = Visibility.Visible;
QueryGrid.Height = Double.NaN;
QueryGrid.UpdateLayout();
}
}
else
{
QueryDetailsDataGrid.ItemsSource = null ;
DataGridScrollViewer.Visibility = Visibility.Collapsed;
QueryGrid.Height = Double.NaN;
QueryGrid.UpdateLayout();
}
}
private void QueryTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Query failed: " + args.Error);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
Partial Public Class AttributeQuery
Inherits UserControl
Public Sub New ()
InitializeComponent()
Dim queryTask As New QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" )
AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler queryTask.Failed, AddressOf QueryTask_Failed
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
' Specify fields to return from initial query
query.OutFields.AddRange(New String () {"STATE_NAME" })
' This query will just populate the drop-down, so no need to return geometry
query.ReturnGeometry = False
' Return all features
query.Where = "1=1"
queryTask.ExecuteAsync(query, "initial" )
End Sub
Private Sub QueryComboBox_SelectionChanged(sender As Object , e As SelectionChangedEventArgs)
If QueryComboBox.SelectedItem.ToString().Contains("Select..." ) Then
Return
End If
Dim queryTask As New QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" )
AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler queryTask.Failed, AddressOf QueryTask_Failed
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
query.OutFields.Add("*" )
query.ReturnGeometry = True
query.Text = QueryComboBox.SelectedItem.ToString()
query.OutSpatialReference = MyMap.SpatialReference
queryTask.ExecuteAsync(query)
End Sub
Private Sub QueryTask_ExecuteCompleted(sender As Object , args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
Dim featureSet As FeatureSet = args.FeatureSet
' If initial query to populate states combobox
If TryCast(args.UserState, String ) = "initial" Then
' Just show on initial load
QueryComboBox.Items.Add("Select..." )
For Each graphic As Graphic In args.FeatureSet.Features
QueryComboBox.Items.Add(graphic.Attributes("STATE_NAME" ).ToString())
Next
QueryComboBox.SelectedIndex = 0
Return
End If
' Remove the first entry if "Select..."
If QueryComboBox.Items(0).ToString().Contains("Select..." ) Then
QueryComboBox.Items.RemoveAt(0)
End If
' If an item has been selected
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer" ), GraphicsLayer)
graphicsLayer.ClearGraphics()
If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
' Show selected feature attributes in DataGrid
Dim selectedFeature As Graphic = featureSet.Features(0)
QueryDetailsDataGrid.ItemsSource = selectedFeature.Attributes
' Hightlight selected feature
selectedFeature.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol" ), ESRI.ArcGIS.Client.Symbols.Symbol)
graphicsLayer.Graphics.Add(selectedFeature)
' Zoom to selected feature (define expand percentage)
Dim selectedFeatureExtent As ESRI.ArcGIS.Client.Geometry.Envelope = selectedFeature.Geometry.Extent
Dim expandPercentage As Double = 30
Dim widthExpand As Double = selectedFeatureExtent.Width * (expandPercentage / 100)
Dim heightExpand As Double = selectedFeatureExtent.Height * (expandPercentage / 100)
Dim displayExtent As New ESRI.ArcGIS.Client.Geometry.Envelope(selectedFeatureExtent.XMin - (widthExpand / 2), selectedFeatureExtent.YMin - (heightExpand / 2), selectedFeatureExtent.XMax + (widthExpand / 2), selectedFeatureExtent.YMax + (heightExpand / 2))
MyMap.ZoomTo(displayExtent)
' If DataGrid not visible (initial load), show it
If DataGridScrollViewer.Visibility = Visibility.Collapsed Then
DataGridScrollViewer.Visibility = Visibility.Visible
QueryGrid.Height = [Double ].NaN
QueryGrid.UpdateLayout()
End If
Else
QueryDetailsDataGrid.ItemsSource = Nothing
DataGridScrollViewer.Visibility = Visibility.Collapsed
QueryGrid.Height = [Double ].NaN
QueryGrid.UpdateLayout()
End If
End Sub
Private Sub QueryTask_Failed(sender As Object , args As TaskFailedEventArgs)
MessageBox.Show("Query failed: " & Convert.ToString(args.[Error ]))
End Sub
End Class
End Namespace
5/16/2014