Select to view content in your preferred language

ListBox SelectionChanged zoom to index??

941
6
08-29-2012 11:31 PM
BrianHjort_Nielsen
Occasional Contributor
Hi,

im having a problem with Zooming to a populated ListBox, i have a query that populates the Listbox and Zooms to the first object in the ListBox, but when i try to click on another object in the Box, nothing happens.

I used to have my SelectionChanged event to route back to my "QueryTask_ExecuteComplete" and that worked, but the problem with this was, that it added objects to my ListBox every time i clicked on an object in it, and if i tried to do a whole new Query, it crashed if i had clicked on an object in the ListBox.

this is my "QueryTask_ExecuteComplete":

void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            //query.Text = comboBox1.SelectedItem.ToString();
            FeatureSet featureSet = args.FeatureSet;

            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();
                                
            foreach (Graphic graphic in args.FeatureSet.Features)
               
                comboBox1.Items.Add(graphic.Attributes["Name"].ToString());
          

            if (featureSet != null && featureSet.Features.Count > 0)
            {
                Graphic selectedfeature = featureSet.Features[0];
                //QueryDetailsDataGrid.ItemsSource = featureSet.Features;
                //QueryListbox.ItemsSource = featureSet.Features;

                selectedfeature.Symbol = LayoutRoot.Resources["defaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                graphicsLayer.Graphics.Add(selectedfeature);

                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 / 1),
                selectedFeatureExtent.YMin - (heightExpand / 1),
                selectedFeatureExtent.XMax + (widthExpand / 1),
                selectedFeatureExtent.YMax + (heightExpand / 1));

                MyMap.ZoomTo(displayExtent);

            }

        }

THIS is my somewhat working SelectionChanged:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (comboBox1.SelectedItem.ToString().Contains("Name"))
                //return;

            QueryTask queryTask = new QueryTask("http://hkn082/ArcGIS/rest/services/Baskarta/Fastighetsytor/MapServer/2");
            queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
            queryTask.Failed += QueryTask_Failed;

            ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
            query.ReturnGeometry = true;
            //query.Text = comboBox1.SelectedItem.ToString();
            query.OutSpatialReference = MyMap.SpatialReference;
            //query.OutFields.Add("");

            queryTask.ExecuteAsync(query);
        }

Any help would be helpful and appreciated
0 Kudos
6 Replies
SanajyJadhav
Deactivated User
If I understand correctly,you are populating ComboBox with the values which you got as a result of query.And, you want to zoom to selected item in the ListBox.If this is right, I have done something similar to this as below.

1. Created class that exposes two properties, first is name and other is geometry. Created private List<ThisClass> to hold instances of my class.
2. In the first query completed event, I had created instance of this class ,set its property name by "(graphic.Attributes["Name"].ToString());". And the geometry property by graphic.Geometry.Added this instance to the List.
3. Populated the ComboBox by this name property.
4. In the Combobox selection changed event, I would iterate thru this list, check name property and if name matches, I would use Geometry property of that instance to set the extent of the map.So ,no need to execute another query.

hope, this helps.
0 Kudos
BrianHjort_Nielsen
Occasional Contributor
If I understand correctly,you are populating ComboBox with the values which you got as a result of query.And, you want to zoom to selected item in the ListBox.If this is right, I have done something similar to this as below.

1. Created class that exposes two properties, first is name and other is geometry. Created private List<ThisClass> to hold instances of my class.
2. In the first query completed event, I had created instance of this class ,set its property name by "(graphic.Attributes["Name"].ToString());". And the geometry property by graphic.Geometry.Added this instance to the List.
3. Populated the ComboBox by this name property.
4. In the Combobox selection changed event, I would iterate thru this list, check name property and if name matches, I would use Geometry property of that instance to set the extent of the map.So ,no need to execute another query.

hope, this helps.


Thanks for your reply!

Yes with one exception, im using a listbox instead, in my code i have changed the x:name of my listbox to that of an ComboBox i used to have in my build. ( i replaced the ComboBox with a ListBox).

do you have an example of the code, i tried something like what you say, but didnt get it to work...
0 Kudos
SanajyJadhav
Deactivated User
I'll put my code here tomorrow when I reach the office.
0 Kudos
SanajyJadhav
Deactivated User
.This is the class that saved the queried features.
 public class IdedFeatures
    {
        public string LayerName { get; set; }

        public IDictionary<string, Object> AttrData { get; set; }

        public string DisplayString { get; set; }

        public Graphic idedGraphic { get; set; }
    }


Below should be your query completed event.

 if (e.IdentifyResults != null && e.IdentifyResults.Count > 0)
                {
                    //List that will hold the all identified features as an instance our custom class "IdedFeatures"
                    m_ListIdedFeatures = new List<IdedFeatures>();

    //loop thru each identified feature,create instance of the class "IdedFeatures", set its properties
                    //using IdentifiedFeature and add this instance to its list.
                    foreach (IdentifyResult idResult in e.IdentifyResults)
                    {
                        IdedFeatures idedFeat = new IdedFeatures();
                        idedFeat.LayerName = idResult.LayerName;
                        idedFeat.AttrData = idResult.Feature.Attributes;
                        idedFeat.idedGraphic = idResult.Feature;
                        idedFeat.DisplayString = idResult.LayerName + " : " + idResult.Value;

                        m_ListIdedFeatures.Add(idedFeat);
                    }

  //set datasource of combo box
cmbIdentifyResult.ItemsSource = null;
cmbIdentifyResult.ItemsSource = m_ListIdedFeatures;
}


Below is the selection changed event of my Combo. Your's would be for the ListBox.

//cast selected combo box item to the type of IdedFeatures so that we can have reference to the Graphic of the selected item.
    IdedFeatures selectedFeature = cmbIdentifyResult.SelectedItem as IdedFeatures;

//zoom to the extent of the extent of the selected item in the listbox
  MapControl.Extent = selectedFeature.idedGraphic.Geometry.Extent;


Let me know if this helps you.

Thanks.
0 Kudos
BrianHjort_Nielsen
Occasional Contributor
Hi again, and thanks!

I dont know if i am missing something or a reference, but i can't get the code to work, problems starts when i try to debug the code, what does your class header look like, my "querytask_executecompleted" is a "ESRI.ArcGIS.Client.Tasks.QueryEventArgs". and when i try out your code example i get the message that the arguments in your code cant be found in the class... =/
0 Kudos
SanajyJadhav
Deactivated User
Can you paste your code with point where code breaks here? Also, let me know the error message you are seeing.
0 Kudos