This sample demonstrates performing find operations. To use the sample, specify a search string in the TextBox Control and click the Find Button. Click on rows in the DataGrid at the bottom of the application to see them in the map.
In the code-behind, the sample uses a FindTask to perform the find operation. The task's find parameters specify to search specific fields in the states and counties layers.
<UserControlx:Class="ArcGISWPFSDK.Find"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"><Gridx:Name="LayoutRoot"Background="#FF5C90B2"><Grid.Resources><esri:SimpleMarkerSymbolx:Key="DefaultMarkerSymbol"Size="8"Color="Red"Style="Circle"/><esri:SimpleLineSymbolx:Key="DefaultLineSymbol"Color="Red"Width="6"/><esri:SimpleFillSymbolx:Key="DefaultFillSymbol"BorderBrush="Red"BorderThickness="2"Fill="#50FF0000"/><LinearGradientBrushx:Key="PanelGradient"EndPoint="0.5,1"StartPoint="0.5,0"><LinearGradientBrush.RelativeTransform><TransformGroup><ScaleTransformCenterY="0.5"CenterX="0.5"/><SkewTransformCenterY="0.5"CenterX="0.5"/><RotateTransformAngle="176"CenterY="0.5"CenterX="0.5"/><TranslateTransform/></TransformGroup></LinearGradientBrush.RelativeTransform><GradientStopColor="#FF145787"Offset="0.16"/><GradientStopColor="#FF3D7FAC"Offset="0.502"/><GradientStopColor="#FF88C5EF"Offset="0.984"/></LinearGradientBrush></Grid.Resources><Grid.RowDefinitions><RowDefinitionHeight="35"/><RowDefinitionHeight="*"/><RowDefinitionHeight="150"/></Grid.RowDefinitions><BorderBorderBrush="Black"BorderThickness="1"><StackPanelOrientation="Horizontal"Background="{StaticResource PanelGradient}"Grid.Row="0"HorizontalAlignment="Stretch"VerticalAlignment="Top"><TextBlockText="Search for"Foreground="White"HorizontalAlignment="Center"Height="24"VerticalAlignment="Center"FontWeight="Bold"FontSize="12"Margin="20,8,5,0"/><TextBoxx:Name="FindText"Background="White"Text="Colorado"Height="23"Width="100"HorizontalContentAlignment="Center"/><TextBlockText="in the attributes of States, Rivers, or Cities:"Foreground="White"HorizontalAlignment="Center"Height="24"VerticalAlignment="Center"FontWeight="Bold"FontSize="12"Margin="5,8,5,0"/><Buttonx:Name="ExecuteButton"Content="Find"Width="75"Height="24"VerticalAlignment="Center"Click="ExecuteButton_Click"Margin="5,0,5,0"Cursor="Hand"/></StackPanel></Border><esri:Mapx:Name="MyMap"Grid.Row="1"WrapAround="True"Extent="-15000000,2000000,-7000000,8000000"><esri:ArcGISTiledMapServiceLayerID="TopoLayer"Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/><esri:ArcGISDynamicMapServiceLayerID="DemographicLayer"Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"/></esri:Map><DataGridx:Name="FindDetailsDataGrid"AutoGenerateColumns="False"HeadersVisibility="All"Background="White"BorderBrush="Black"BorderThickness="1"SelectionChanged="FindDetails_SelectionChanged"HorizontalScrollBarVisibility="Hidden"Grid.Row="2"IsReadOnly="True"HorizontalAlignment="Stretch"VerticalAlignment="Stretch"Height="Auto"Width="Auto"><DataGrid.Columns><DataGridTextColumnBinding="{Binding Path=LayerId}"Header="Layer ID"/><DataGridTextColumnBinding="{Binding Path=LayerName}"Header="Layer Name"/><DataGridTextColumnBinding="{Binding Path=FoundFieldName}"Header="Found Field Name"/><DataGridTextColumnBinding="{Binding Path=Value}"Header="Found Field Value"/></DataGrid.Columns></DataGrid></Grid></UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
publicpartialclass Find : UserControl
{
public Find()
{
InitializeComponent();
}
privatevoid ExecuteButton_Click(object sender, RoutedEventArgs e)
{
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
FindTask findTask = new FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer");
findTask.Failed += FindTask_Failed;
FindParameters findParameters = new FindParameters();
// Layer ids to search
findParameters.LayerIds.AddRange(newint[] { 0, 1, 2 });
// Fields in layers to search
findParameters.SearchFields.AddRange(newstring[] { "CITY_NAME", "NAME", "SYSTEM", "STATE_ABBR", "STATE_NAME" });
// Return features in map's spatial reference
findParameters.SpatialReference = MyMap.SpatialReference;
// Bind data grid to find results. Bind to the LastResult property which returns a list// of FindResult instances. When LastResult is updated, the ItemsSource property on the // will update.
Binding resultFeaturesBinding = new Binding("LastResult");
resultFeaturesBinding.Source = findTask;
FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
findParameters.SearchText = FindText.Text;
findTask.ExecuteAsync(findParameters);
// Since binding to DataGrid, handling the ExecuteComplete event is not necessary.
}
privatevoid FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Highlight the graphic feature associated with the selected row
DataGrid dataGrid = sender as DataGrid;
int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;
Graphic graphic = findResult.Feature;
switch (graphic.Attributes["Shape"].ToString())
{
case"Polygon":
graphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
break;
case"Polyline":
graphic.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
break;
case"Point":
graphic.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
break;
}
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
graphicsLayer.Graphics.Add(graphic);
}
}
privatevoid FindTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Find failed: " + args.Error);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
PartialPublicClass Find
Inherits UserControl
PublicSubNew()
InitializeComponent()
EndSubPrivateSub ExecuteButton_Click(sender AsObject, e As RoutedEventArgs)
Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
Dim findTask AsNew FindTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer")
AddHandler findTask.Failed, AddressOf FindTask_Failed
Dim findParameters AsNew FindParameters()
' Layer ids to search
findParameters.LayerIds.AddRange(NewInteger() {0, 1, 2})
' Fields in layers to search
findParameters.SearchFields.AddRange(NewString() {"CITY_NAME", "NAME", "SYSTEM", "STATE_ABBR", "STATE_NAME"})
' Return features in map's spatial reference
findParameters.SpatialReference = MyMap.SpatialReference
' Bind data grid to find results. Bind to the LastResult property which returns a list' of FindResult instances. When LastResult is updated, the ItemsSource property on the ' will update. Dim resultFeaturesBinding AsNew Binding("LastResult")
resultFeaturesBinding.Source = findTask
FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding)
findParameters.SearchText = FindText.Text
findTask.ExecuteAsync(findParameters)
' Since binding to DataGrid, handling the ExecuteComplete event is not necessary.EndSubPrivateSub FindDetails_SelectionChanged(sender AsObject, e As SelectionChangedEventArgs)
' Highlight the graphic feature associated with the selected rowDim dataGrid As DataGrid = TryCast(sender, DataGrid)
Dim selectedIndex AsInteger = dataGrid.SelectedIndex
If selectedIndex > -1 ThenDim findResult As FindResult = DirectCast(FindDetailsDataGrid.SelectedItem, FindResult)
Dim graphic As Graphic = findResult.Feature
SelectCase graphic.Attributes("Shape").ToString()
Case"Polygon"
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
ExitSelectCase"Polyline"
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultLineSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
ExitSelectCase"Point"
graphic.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
ExitSelectEndSelectDim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
graphicsLayer.ClearGraphics()
graphicsLayer.Graphics.Add(graphic)
EndIfEndSubPrivateSub FindTask_Failed(sender AsObject, args As TaskFailedEventArgs)
MessageBox.Show("Find failed: " & Convert.ToString(args.[Error]))
EndSubEndClassEndNamespace