原文地址:http://hi.baidu.com/javacookies/blog/item/aef559a114d6e685461064a8.html
<UserControl x:Class="ArcGISSilverlightSDK.Find"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<Grid x:Name="LayoutRoot" Background="#FF5C90B2">
<Grid.Resources>
<esriSymbols:SimpleMarkerSymbol x:Name="DefaultMarkerSymbol" Size="8" Color="Red" Style="Circle" />
<esriSymbols:SimpleLineSymbol x:Name="DefaultLineSymbol" Color="Red" Width="6" />
<esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" BorderBrush="Red" BorderThickness="2" Fill="#50FF0000"/>
<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="*" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<!--查询条件-->
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel Orientation="Horizontal" Background="#FF5C90B2" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock Text="Search for" Foreground="Black"
HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
FontWeight="Bold" FontSize="12" Margin="20,8,5,0"/>
<TextBox x:Name="FindText" Background="White" Text="River" Height="23" Width="100" HorizontalContentAlignment="Center" />
<TextBlock Text="in the attributes of States or Counties:" Foreground="Black"
HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
FontWeight="Bold" FontSize="12" Margin="5,8,5,0"/>
<Button x:Name="ExecuteButton" Content="Find" Width="75" Height="24" VerticalAlignment="Center" Click="ExecuteButton_Click"
Margin="5,0,5,0" Cursor="Hand" />
</StackPanel>
</Border>
<!--地图控件-->
<esri:Map x:Name="MyMap" Background="White" Grid.Row="1" Extent="-125, 25, -65, 55">
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="TopoLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/NGS_Topo_US_2D/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer ID="DemographicLayer" Opacity="0.2"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" VisibleLayers="4,5" />
<esri:GraphicsLayer ID="MyGraphicsLayer" />
</esri:Map.Layers>
</esri:Map>
<!--查询结果GIRD控件-->
<slData:DataGrid x:Name="FindDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All" Background="White"
BorderBrush="Black" BorderThickness="1" SelectionChanged="FindDetails_SelectionChanged"
HorizontalAlignment="Left" HorizontalScrollBarVisibility="Hidden" Grid.Row="2" Width="Auto"
IsReadOnly="True">
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Width="65" Binding="{Binding Path=LayerId}" Header="Layer ID" />
<slData:DataGridTextColumn Width="90" Binding="{Binding Path=LayerName}" Header="Layer Name"/>
<slData:DataGridTextColumn Width="80" Binding="{Binding Path=FoundFieldName}" Header="Found Field" />
<slData:DataGridTextColumn Width="80" Binding="{Binding Path=Value}" Header="Value"/>
<slData:DataGridTextColumn Width="120" Binding="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay, Path=Feature.Attributes}" Header="State Name"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
</Grid>
</UserControl>
<UserControl x:Class="ArcGISSilverlightSDK.Find"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:esriConverters="clr-namespace:ESRI.ArcGIS.Client.ValueConverters;assembly=ESRI.ArcGIS.Client"
xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">
<Grid x:Name="LayoutRoot" Background="#FF5C90B2">
<Grid.Resources>
<esriSymbols:SimpleMarkerSymbol x:Name="DefaultMarkerSymbol" Size="8" Color="Red" Style="Circle" />
<esriSymbols:SimpleLineSymbol x:Name="DefaultLineSymbol" Color="Red" Width="6" />
<esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" BorderBrush="Red" BorderThickness="2" Fill="#50FF0000"/>
<esriConverters:DictionaryConverter x:Name="MyDictionaryConverter" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="*" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<!--查询条件-->
<Border BorderBrush="Black" BorderThickness="1">
<StackPanel Orientation="Horizontal" Background="#FF5C90B2" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" >
<TextBlock Text="Search for" Foreground="Black"
HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
FontWeight="Bold" FontSize="12" Margin="20,8,5,0"/>
<TextBox x:Name="FindText" Background="White" Text="River" Height="23" Width="100" HorizontalContentAlignment="Center" />
<TextBlock Text="in the attributes of States or Counties:" Foreground="Black"
HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
FontWeight="Bold" FontSize="12" Margin="5,8,5,0"/>
<Button x:Name="ExecuteButton" Content="Find" Width="75" Height="24" VerticalAlignment="Center" Click="ExecuteButton_Click"
Margin="5,0,5,0" Cursor="Hand" />
</StackPanel>
</Border>
<!--地图控件-->
<esri:Map x:Name="MyMap" Background="White" Grid.Row="1" Extent="-125, 25, -65, 55">
<esri:Map.Layers>
<esri:ArcGISTiledMapServiceLayer ID="TopoLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/NGS_Topo_US_2D/MapServer"/>
<esri:ArcGISDynamicMapServiceLayer ID="DemographicLayer" Opacity="0.2"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" VisibleLayers="4,5" />
<esri:GraphicsLayer ID="MyGraphicsLayer" />
</esri:Map.Layers>
</esri:Map>
<!--查询结果GIRD控件-->
<slData:DataGrid x:Name="FindDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All" Background="White"
BorderBrush="Black" BorderThickness="1" SelectionChanged="FindDetails_SelectionChanged"
HorizontalAlignment="Left" HorizontalScrollBarVisibility="Hidden" Grid.Row="2" Width="Auto"
IsReadOnly="True">
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Width="65" Binding="{Binding Path=LayerId}" Header="Layer ID" />
<slData:DataGridTextColumn Width="90" Binding="{Binding Path=LayerName}" Header="Layer Name"/>
<slData:DataGridTextColumn Width="80" Binding="{Binding Path=FoundFieldName}" Header="Found Field" />
<slData:DataGridTextColumn Width="80" Binding="{Binding Path=Value}" Header="Value"/>
<slData:DataGridTextColumn Width="120" Binding="{Binding Converter={StaticResource MyDictionaryConverter},
ConverterParameter=STATE_NAME, Mode=OneWay, Path=Feature.Attributes}" Header="State Name"/>
</slData:DataGrid.Columns>
</slData:DataGrid>
</Grid>
</UserControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISSilverlightSDK
{
public partial class Find : UserControl
{
public Find()
{
InitializeComponent();
}
//查询
private void 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/" +
"Demographics/ESRI_Census_USA/MapServer");
findTask.Failed += FindTask_Failed;
FindParameters findParameters = new FindParameters();
// 查询的层列表
findParameters.LayerIds.AddRange(new int[] { 3, 5 });
// 查询的字段列表
findParameters.SearchFields.AddRange(new string[] { "NAME", "STATE_NAME", "STATE_ABBR", "TRACT", "BLOCK", "BLKGRP" });
// 绑定查询结果到GRID
Binding resultFeaturesBinding = new Binding("LastResult");
resultFeaturesBinding.Source = findTask;
FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
//查询条件
findParameters.SearchText = FindText.Text;
//执行查询
findTask.ExecuteAsync(findParameters);
}
private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//高亮显示被选中记录对应的图元
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 = DefaultFillSymbol;
break;
case "Polyline":
graphic.Symbol = DefaultLineSymbol;
break;
case "Point":
graphic.Symbol = DefaultMarkerSymbol;
break;
}
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
graphicsLayer.Graphics.Add(graphic);
}
}
private void FindTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Find failed: " + args.Error.Message);
}
}
}
using System.Windows.Data;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISSilverlightSDK
{
public partial class Find : UserControl
{
public Find()
{
InitializeComponent();
}
//查询
private void 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/" +
"Demographics/ESRI_Census_USA/MapServer");
findTask.Failed += FindTask_Failed;
FindParameters findParameters = new FindParameters();
// 查询的层列表
findParameters.LayerIds.AddRange(new int[] { 3, 5 });
// 查询的字段列表
findParameters.SearchFields.AddRange(new string[] { "NAME", "STATE_NAME", "STATE_ABBR", "TRACT", "BLOCK", "BLKGRP" });
// 绑定查询结果到GRID
Binding resultFeaturesBinding = new Binding("LastResult");
resultFeaturesBinding.Source = findTask;
FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
//查询条件
findParameters.SearchText = FindText.Text;
//执行查询
findTask.ExecuteAsync(findParameters);
}
private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//高亮显示被选中记录对应的图元
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 = DefaultFillSymbol;
break;
case "Polyline":
graphic.Symbol = DefaultLineSymbol;
break;
case "Point":
graphic.Symbol = DefaultMarkerSymbol;
break;
}
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
graphicsLayer.ClearGraphics();
graphicsLayer.Graphics.Add(graphic);
}
}
private void FindTask_Failed(object sender, TaskFailedEventArgs args)
{
MessageBox.Show("Find failed: " + args.Error.Message);
}
}
}