Select to view content in your preferred language

Problem to zoom to selected graphic for the feature datagrid.

1281
3
03-21-2011 01:21 PM
weiliang
Deactivated User
Hi there,

I have a feature data grid with a feature layer binding to it. I am trying to code to let user click on one item in this feature data grid, and the map zoom to this selected feature. However, in the SelectionChanged event handler, the FeatureLayerObject.SelectedGraphics always returns the previous feature been selected (i.e., first time, the SelectedGraphics after click on any item in the feature data grid is null, and second time the SelectedGraphics returns the first feature that was just been selected). This makes that I can't get the geometry of current selected feature form this SelectedGraphics attribute. Has anyone encounter the same problem before? 

Thanks for your help in advance!

Wei
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
If you subscribe to FeatureDataGrid.SelectionChanged event, be sure to use FeatureDataGrid.SelectedGraphics.

You can try the following code with SDK sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureDataGrid. Notice that the OBJECTID's of current data grid selection are printed on your OutputWindow.
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 if (MyDataGrid == null || MyDataGrid.SelectedGraphics == null) return;
 System.Diagnostics.Debug.WriteLine("begin selection");
 foreach (var g in MyDataGrid.SelectedGraphics)
  System.Diagnostics.Debug.WriteLine(g.Attributes["OBJECTID"]);
 System.Diagnostics.Debug.WriteLine("end selection");
}
0 Kudos
MarkusHjärne
Deactivated User
Hi,

I have just experienced the same behavior as wliang1. In the handler for FeatureDataGrid.SelectionChanged the FeatureDataGrid.SelectedGraphics collection seems not to have been updated yet, so it holds the previous selection.

I've just upgraded an application to version 2.4 of the Silverlight API, and I'm pretty sure that this behavior has changed since version 2.1.

A workaround that worked for me was to wrap all code in the handler in Dispatcher.BeginInvoke(), like this:

private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 Dispatcher.BeginInvoke(() =>
 {
  if (MyDataGrid == null || MyDataGrid.SelectedGraphics == null) return;
  System.Diagnostics.Debug.WriteLine("begin selection");
  foreach (var g in MyDataGrid.SelectedGraphics)
   System.Diagnostics.Debug.WriteLine(g.Attributes["OBJECTID"]);
  System.Diagnostics.Debug.WriteLine("end selection");
 });
}.


Hope this can be of some help to you.

/Markus
0 Kudos
JenniferNery
Esri Regular Contributor
This related thread might also be helpful: http://forums.arcgis.com/threads/46013-FeatureDataGrid-Selection-Changed-Event. You can use e.AddedItems (which contains the selected row) and convert to graphic (see post# 2).
0 Kudos