This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform an attribute query on local data, add the
results to the map and a DataGrid, and zoom 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 local map service 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.LocalAttributeQuery "
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 >
< BooleanToVisibilityConverter x:Key = " BooleanToVisibilityConverter " />
< esri : SimpleFillSymbol x:Key = " DefaultFillSymbol " Fill = " #500000FF " BorderBrush = " Blue " BorderThickness = " 1 " />
< esri : PictureMarkerSymbol x:Key = " DefaultPictureSymbol " OffsetX = " 35 " OffsetY = " 35 "
Source = " \Assets\Images\i_about.png " />
< 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 " WrapAround = " True " Extent = " -15000000,2000000,-7000000,8000000 " Background = " #FFE3E3E3 " MinimumResolution = " 2445.98490512499 " >
< esri : Map.Layers >
< esri : ArcGISLocalTiledLayer ID = " BaseMap " Path = " ..\\Data\\TPKs\\Topographic.tpk " />
< esri : ArcGISLocalDynamicMapServiceLayer Path = " ..\\Data\\MPKS\\USCitiesStates.mpk " />
< esri : GraphicsLayer ID = " MyGraphicsLayer " />
</ esri : Map.Layers >
</ 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 >
< ProgressBar x:Name = " MyProgressBar " IsIndeterminate = " True " VerticalAlignment = " Bottom " Width = " 200 " Height = " 20 " Margin = " 10 " Visibility = " {Binding Path=IsBusy, Converter={StaticResource BooleanToVisibilityConverter}} " > </ ProgressBar >
</ Grid >
</ UserControl >
using System;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Local;
using ESRI.ArcGIS.Client;
using System.ComponentModel;
namespace ArcGISWPFSDK
{
public partial class LocalAttributeQuery : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _isBusy = true ;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
_isBusy = value;
if (PropertyChanged != null )
{
PropertyChanged(this , new PropertyChangedEventArgs("IsBusy" ));
}
}
}
LocalMapService _mapService;
public LocalAttributeQuery()
{
InitializeComponent();
QueryComboBox.IsEnabled = false ;
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk" , (localMapService) =>
{
_mapService = localMapService;
QueryComboBox.IsEnabled = true ;
QueryTask queryTask = new QueryTask();
queryTask.Url = _mapService.UrlMapService + "/2" ;
queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
queryTask.Failed += QueryTask_Failed;
ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
query.ReturnGeometry = false ;
query.Where = "1=1" ;
queryTask.ExecuteAsync(query, "initial" );
DataContext = this ;
IsBusy = false ;
});
}
private void QueryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (QueryComboBox.SelectedItem.ToString().Contains("Select..." ))
return ;
QueryTask queryTask2 = new QueryTask(_mapService.UrlMapService + "/2" );
queryTask2.ExecuteCompleted += QueryTask_ExecuteCompleted;
queryTask2.Failed += QueryTask_Failed;
ESRI.ArcGIS.Client.Tasks.Query query2 = new ESRI.ArcGIS.Client.Tasks.Query();
query2.OutFields.Add("*" );
query2.Where = "STATE_NAME = '" + QueryComboBox.SelectedItem.ToString() + "'" ;
query2.ReturnGeometry = true ;
queryTask2.ExecuteAsync(query2);
}
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 (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.Tasks
Imports ESRI.ArcGIS.Client.Local
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports System.ComponentModel
Namespace ArcGISWPFSDK
Partial Public Class LocalAttributeQuery
Inherits UserControl
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _isBusy As Boolean = True
Public Property IsBusy() As Boolean
Get
Return _isBusy
End Get
Set (value As Boolean )
_isBusy = value
RaiseEvent PropertyChanged(Me , New PropertyChangedEventArgs("IsBusy" ))
End Set
End Property
Private _mapService As LocalMapService
Public Sub New ()
InitializeComponent()
QueryComboBox.IsEnabled = False
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk" , Function (localMapService__1)
_mapService = localMapService__1
QueryComboBox.IsEnabled = True
Dim queryTask As New QueryTask()
queryTask.Url = _mapService.UrlMapService & "/2"
AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler queryTask.Failed, AddressOf QueryTask_Failed
Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
query.ReturnGeometry = False
query.Where = "1=1"
queryTask.ExecuteAsync(query, "initial" )
DataContext = Me
IsBusy = False
End Function )
End Sub
Private Sub QueryComboBox_SelectionChanged(sender As Object , e As SelectionChangedEventArgs)
If QueryComboBox.SelectedItem.ToString().Contains("Select..." ) Then
Return
End If
Dim queryTask2 As New QueryTask(_mapService.UrlMapService & "/2" )
AddHandler queryTask2.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler queryTask2.Failed, AddressOf QueryTask_Failed
Dim query2 As New ESRI.ArcGIS.Client.Tasks.Query()
query2.OutFields.Add("*" )
query2.Where = "STATE_NAME = '" & QueryComboBox.SelectedItem.ToString() & "'"
query2.ReturnGeometry = True
queryTask2.ExecuteAsync(query2)
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 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