Select to view content in your preferred language

Zoom to and select the featues

2417
6
09-29-2011 11:59 PM
ManojrajTeli
Deactivated User
After executing the query i am binding the result returned from query into feature data grid.Now my problem is that whenever i select the row from featuredatagrid or click to zoom to selected feature then nothing happens.Is there any way to make it zoom to selected feature from the feature data grid.Thank you in advance.
0 Kudos
6 Replies
HugoCardenas
Emerging Contributor
manojraj,
I have being doing this all along.  This is one way to do it:

First, define a "SelectionChanged" event for the feature data grid as follows:

C#:

private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs args)
{...}


XAML:
<esri:FeatureDataGrid  x:Name="MyDataGrid" SelectionChanged="MyDataGrid_SelectionChanged" ... </esri:FeatureDataGrid>


Now, inside the "MyDataGrid_SelectionChanged" event handler, retrieve the last selected graphic:

 IEnumerable<Graphic> myGraphics = MyDataGrid.SelectedGraphics;
 Graphic graphic = myGraphics.Last<Graphic>();
MyMap.ZoomTo(graphic.Geometry);


You may need to pan to it, if needed; if so:

MyMap.PanTo(graphic.Geometry);

Hope this helps!

Hugo.
0 Kudos
ManojrajTeli
Deactivated User
Thanks for the reply Hugo.I am loading the feature using this code is it going to work.
[HTML]
void queryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs e)
        {
            GraphicsLayer graphicsLayer = Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();
            FeatureSet featureSet = e.FeatureSet;
           
            if (featureSet != null && featureSet.Features.Count > 0)
            {  
                foreach (Graphic resultFeature in featureSet.Features)
                {                   
                    graphicsLayer.Graphics.Add(resultFeature);
                }

                System.Windows.Data.Binding bind = new System.Windows.Data.Binding();
                bind.ElementName = "Map";

                bind.Path = new PropertyPath("Layers.[MyGraphicsLayer]");               
                MyDataGrid.SetBinding(ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid.GraphicsLayerProperty, bind);
                MyDataGrid.UpdateLayout();      
            }
            else
            {
                MessageBox.Show("No features Returned from Query", "Query", MessageBoxButton.OK);
                MyDataGrid.Columns.Clear();
                foreach (KeyValuePair<string, string> kv in featureSet.FieldAliases)
                {
                    DataGridTextColumn dt = new DataGridTextColumn();
                    Binding b = new Binding("Attributes");
                    b.Converter = new ESRI.ArcGIS.Client.ValueConverters.DictionaryConverter();
                    b.ConverterParameter = kv.Key;
                    dt.Binding = b;
                    dt.Header = kv.Value;                   
                }     
            }
           
        }
[/HTML]



After adding your code the silverlight control just disappeared and whole screen becomes blank.I am
using following code


[HTML]
<esri:FeatureDataGrid VerticalAlignment="Bottom" IsReadOnly="True" HorizontalAlignment="Stretch" x:Name="MyDataGrid"  Height="162" Margin="0,0,1,2" GraphicsLayer="{Binding ElementName=Map, Path=Layers.[Sewer_Lines]}" BorderBrush="Black" BorderThickness="1" Grid.RowSpan="2" SelectionChanged="MyDataGrid_SelectionChanged" MouseLeftButtonDown="MyDataGrid_MouseLeftButtonDown"></esri:FeatureDataGrid>
[/HTML]
0 Kudos
JenniferNery
Esri Regular Contributor
The simplest solution would be to check Auto Zoom to Selected under Options: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureDataGrid. Any row selection in the FeatureDataGrid will trigger the map to zoom to the selected features' extent.
0 Kudos
ManojrajTeli
Deactivated User
Thank you Jennifer for the reply but Auto Zoom to Selected feature is not working .
0 Kudos
HugoCardenas
Emerging Contributor
manojraj,
Sorry, I had not taken a look at your replies....

I am not sure as to why your screen goes blank, but I see some issues:

1.  Do not declare the event MouseLeftButtonDown="MyDataGrid_MouseLeftButtonDown" on the feature data grid.  Declare it with the feature layer instead (cf. XAML code below):

<esri:FeatureLayer ID="Sewer_Lines" Renderer="{StaticResource Your-own-Renderer}" Url="Your-own-url" OutFields="*" MouseLeftButtonDown="FeatureLayer_MouseLeftButtonDown">


2. No need to re-bind the feature layer to the feature data grid at run-time once you have bound it in XAML.  You would only need:

<esri:FeatureDataGrid  x:Name="MyDataGrid" Map="{Binding ElementName=MyMap}"
GraphicsLayer="{Binding ElementName=MyMap, Path=Layers.[Sewer_Lines]}" SelectionChanged="MyDataGrid_SelectionChanged" />


Now, the way it works is like this: When you click a feature of "Sewer_Lines", the "MyDataGrid_SelectionChanged" is fired first.  Weird, but even though you are actually clicking on the map screen, this event fires, for it is bound to the feature layer.  Then, you get hold of the selected feature (graphic) here and set your zoom extent to it.

Also, when you click a row in the feature data grid, the "MyDataGrid_SelectionChanged" is fired, of course.  Again, get hold of the selected feature (graphic) here and set your zoom extent to it; like this in the "MyDataGrid_SelectionChanged":

IEnumerable<Graphic> myGraphics = MyDataGrid.SelectedGraphics;
Graphic graphic = myGraphics.Last<Graphic>();
MyMap.ZoomTo(graphic.Geometry);


Here, you get the last graphic in the collection, for there may be several selected features (whenever you click on a feature or row, a feature is selected if it was not previously selected).

Let me know how it goes.  I know what is to be stuck, for I have been there many times....
In these situations, a piece of code demonstrating the issue helps a lot.

Hugo.
0 Kudos
JenniferNery
Esri Regular Contributor
Thank you Jennifer for the reply but Auto Zoom to Selected feature is not working .


Are you using GraphicsLayer or FeatureLayer? What type of geometry does your layer have? It would be nice to know why this fails.

On your MouseLeftButtonDown event, can you check if e.Graphic.Geometry.SpatialReference is set?

Thanks.
0 Kudos