Select to view content in your preferred language

Selection Mode code behind

977
2
01-25-2012 06:55 PM
NathalieNeagle
Regular Contributor
I'm trying to use some spatial selection buttons with several feature layers and several graphic layers.  I have a basic drop down list box to select the layer that is active and in my code behind I want handle the selection.  I have the graphic layers handled but I'm having trouble with the feature layer.  In my XAML I've set the my FeatureLayer modes to Selection and instead of setting everything in XAML I want to set it in code behind.  I know it is better practice to set it in XAML but I feel I need to do everything in the code behind. 

My question is How do I set the properties for the feature layer in C# (behind):  Binding to MyMap and My LayerID (ex.  Map="{Binding ElementName=MyMap}" LayerIDs="CensusDemographics"), Set SelectionMode, Set Continuous Mode, Set the  Command to Select and the CommandParameter to equal new.

Below is the dismal code I have; I tired to look everything up in the API Ref but I just have such a hard time reading and following the API Ref.  I always thought that would be a great Dev. Summit Session or a U.C. Session.  Probably too basic but us true non-programmers might enjoy it. 

Thanks in Advance.

                        
 

  private void SelectButton_Click(object sender, RoutedEventArgs e)
        {
            if (Layers.SelectedIndex == 0) //Graphic Layer
            {
                MyDrawSurface.DrawMode = DrawMode.Rectangle;
                MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);

            }

            if (Layers.SelectedIndex == 1) //Feature Layer
            {
                
                 //Here's where I'm havin the trouble:
                SelectButton.CommandParameter = SelectionMode.Multiple;
              //   SelectButton.SetBinding;
               
                QueryDetailsDataGrid.GraphicsLayer = Map.Layers["LINES"] as FeatureLayer;
                ResultsDisplay.IsExpanded = true;
            }


        }
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
Are you using Editor.Select command to perform selection? http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsExplicitSave, http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection. Editor.Select accepts the following CommandParameter="new", "add", "remove", "keyboard" http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Editor~Sel.... You will still have the chance to distinguish which layer had selected graphics if you subscribe to EditCompletedEvent. EventArgs include graphic and layer http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Editor+Cha.... You will need to check graphic.IsSelected property though or use layer.SelectedGraphics since all graphics whose selection state were changed (as in "new" selection), are also reported. If you want Editor.Select to only act on subset of GraphicsLayer and FeatureLayer, you can specify their ID's by setting Editor.LayerIDs. Otherwise, setting Editor.Map is sufficient as long as every GraphicsLayer, FeatureLayer have ID property set. Are you using FeatureDataGrid to display the features? If yes, you can use FilterSource to only show SelectedGraphics.
MyDataGrid.FilterSource = l.SelectedGraphics.ToList();
0 Kudos
NathalieNeagle
Regular Contributor
jenniferdnery;166986 wrote:
Are you using Editor.Select command to perform selection? Are you using FeatureDataGrid to display the features? If yes, you can use FilterSource to only show SelectedGraphics.





Jen,

I would like to just use the featureDataGrid and the selection tools I've already developed or grabbed from the SDK examples (right now I'm us trying to hook the spatial selection tools).  I was having trouble with that so the example I gave above I was trying to Edit.Select command but I would prefer not (I guess).  Right now I can populate and select Feature Layers to the FeatureDataGrid but since I'm sending the select features to a graphic layer (MyFeatureDataGrid.GraphicsLayer = MygraphicsLayer;) the featureDataGrid doesn't give me my editing/commit button and this is what is giving me issue. Do I need to create an if statement and say something like if MyActiveLayer.SelectedItem == 1....then set the GraphicLayer to a featurelayer base on the selection and use the Editor.Select command???

 private void MyDrawSurface_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {


            GraphicsLayer selectionGraphicslayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            selectionGraphicslayer.ClearGraphics();

            if (MyActiveLayers.SelectedIndex == 0 || Layers.SelectedIndex == 2 || Layers.SelectedIndex == 3)
            {


                QueryTask MyqueryTask = new QueryTask((Layers.SelectedItem as FeatureLayer).Url);


                MyqueryTask.ExecuteCompleted +=  MyQueryTask_ExecuteCompleted;
                MyqueryTask.Failed += MyQueryTask_Failed;

                Query query = new ESRI.ArcGIS.Client.Tasks.Query();

                query.OutFields.Add("*");

                query.Geometry = args.Geometry;

                query.ReturnGeometry = true;

                MyqueryTask.ExecuteAsync(query);
            }


            if (MyActiveLayers.SelectedIndex == 1)
            {

                QueryTask MyqueryTask = new QueryTask("http://myServer/ArcGIS/rest/services/Trails/FeatureServer/1");


                MyqueryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
                MyqueryTask.Failed += QueryTask_Failed;

                Query query = new ESRI.ArcGIS.Client.Tasks.Query();

                query.OutFields.Add("*");

                query.Geometry = args.Geometry;

  
                query.ReturnGeometry = true;

                queryTask.ExecuteAsync(query);
            }

        }

        private void MyQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
        {
            FeatureSet featureSet = args.FeatureSet;

            if (featureSet == null || featureSet.Features.Count < 1)
            {
                MessageBox.Show("No features retured from query");
                return;
            }



            GraphicsLayer MygraphicsLayer = Map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;


            if (featureSet != null && featureSet.Features.Count > 0)

    

            {
                foreach (Graphic feature in featureSet.Features)
                {

                    switch (featureSet.GeometryType.ToString())
                    {
                        case "Polygon":
                            feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
                            break;
                        case "Polyline":
                            feature.Symbol = LayoutRoot.Resources["CustomGrowLineSymbol"] as LineSymbol;
                            break;
                        case "Point":
                            feature.Symbol = LayoutRoot.Resources["ResultsMarkerSymbol"] as SimpleMarkerSymbol;
                            break;
                    }

                    MygraphicsLayer.Graphics.Insert(0, feature);
                    
                }
                MyFeatureDataGrid.GraphicsLayer = MygraphicsLayer;
                ResultsDisplay.IsExpanded = true;
            }
            MyDrawSurface.IsEnabled = false;

0 Kudos