Select to view content in your preferred language

Attachment Editor with FeatureDataGrid_SelectionChange event

5118
18
01-18-2012 09:20 AM
DavidAshton
Frequent Contributor
I have a basic viewer that allows executive management level staff  to select features and view the information in a FeatureDataGrid.  My end users want to be able to view attachments that are associated with the features so we are looking to add the attachment grid but we want to have it function/interact with the FeatureDataGrid.
Here???s the tasks/steps:

Added a new customized button to the featuredataGrid that opens the attachment editor window.

But what I???m having trouble with is I want to use the FeatureDataGrid_SelectionChanged
Event to active the attachment grid and populate with the feature???s attachment. 

Would you take a look and let me know what is wrong (I'm sure a bunch:)) I've been trying this for hours now and I'm getting really twisted.

Thanks
Dave
 #region AttachmentLinking

        private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            var graphics = (sender as ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid).SelectedGraphics;
            //foreach (Graphic feature in graphics)
            foreach (var g in graphics)
            {
                //// get id 
                //var id = g.Attributes["PICS"];
                //// TODO: retrieve outside data for this id
                //HYPERIDPASS.Text = string.Format("{0}", id);
                //break;

                //foreach (var edit in e.Edits)
                //{
                    //if (feature.Attributes is FeatureLayer)
                    //{
                    //    //if (e.Action == Editor.EditAction.Add || edit.Graphic.Selected)
                        //{
                if (_lastGraphic != null)
                    _lastGraphic.UnSelect();

                g.Select();
                if (g.Selected)
                    QueryDetailsDataGrid.ScrollIntoView(g, null);

                _lastGraphic = g;
              //  MyAttachmentEditor.FeatureLayer = feature.Attributes as FeatureLayer;
                MyAttachmentEditor.FeatureLayer = graphics as FeatureLayer;
                            MyAttachmentEditor.GraphicSource = g;
            
                        //    MyAttachmentEditor.GraphicSource = g.Graphic;
                  //      }
                  //  }
                //}
            }

        }

        #endregion
0 Kudos
18 Replies
DavidAshton
Frequent Contributor
I think you can replace this code

                        graphicsLayer.Graphics.Insert(0, feature);

                        FeatureLayer featurelayer = graphicsLayer as FeatureLayer;
                        MyFeatureDataGrid.GraphicsLayer = featurelayer;


with

                        MyFeatureDataGrid.GraphicsLayer = feature.Layer as FeatureLayer;


It should display features from the Editable FeatureLayer. You need not add them to another GraphicsLayer.



Thanks Jennifer, I'm sorry to ask this cause I feel like it is basic but I'm having trouble with ".Layer" what is Layer?  Do I need to set it.  I tired to just add my FeatureLayer ID from my xaml but it didn't like that.

Also I think I missed a closing bracket for the for loop in my first attempt, so Should this one statement go inside or outside the for loop:

   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;
                        }

                        graphicsLayer.Graphics.Insert(0, feature);
}

                        FeatureLayer featurelayer = graphicsLayer as FeatureLayer;
                        MyFeatureDataGrid.GraphicsLayer = featurelayer;

                    }



Thanks
Dave
0 Kudos
JenniferNery
Esri Regular Contributor
I assume this Graphic.Insert() and MyFeatureDataGrid.GraphicsLayer = featureLayer, happens in Editor.EditCompleted event?

So you would have some code similar to:
   editor.EditCompleted += (s, e) =>
   {
    if (e.Action == Editor.EditAction.Select)
    {
     foreach (var edit in e.Edits)
     {
      var g = edit.Graphic;
      if (g.Selected)
      {
       var l = edit.Layer as FeatureLayer;
       MyFeatureDataGrid.GraphicsLayer = l;
      }
     }
    }
   };


Instead of adding the selected graphic to a new GraphicsLayer and setting FeatureDataGrid.GraphicsLayer with this new layer, use the original FeatureLayer so you can pass to AttachmentEditor.GraphicSource and AttachmentEditor.FeatureLayer on FeatureDataGrid.SelectionChanged event.
0 Kudos
DavidAshton
Frequent Contributor
Right.  Sorry that's where I keeping getting lost because I'm just trying to do all of this in the QueryTask.ExecuteComplete Event, which is getting fired from the Draw Event Arguments Operation are Complete.  So after the Draw Event Arguments Operation is Complete and if the layer is a featurelayer I need to start the Editor.EditCompleted event? instead of the QueryTask?
0 Kudos
JenniferNery
Esri Regular Contributor
You can use Editor.Select to perform selection. This will also run a QueryTask in the background and Editor.EditCompleted event should return the affected graphics: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection, http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsExplicitSave.

<Button Command="{Binding Select}" Margin="2" DataContext="{StaticResource MyEditor}"                           
 CommandParameter="New" Content="New"/>
0 Kudos
DavidAshton
Frequent Contributor
Jennifer thanks for all the replies!

I'm familiar with the Editor.Select example from the SDK http://help.arcgis.com/en/webapi/sil...LayerSelection but I was trying to utilize the spatial selection (Basically spatial selection SDK example) tools I already had in the application. I didn't want to have two selection toolbars one for the feature layer(s) and another for my dynamicmapservice layers. Adding two toolbars would have been easier...maybe;) so now I just took the Editor.Select example and move the XAML code to the C# so I can decide when to use the basic query = MyDrawSurface.DrawMode = DrawMode.Polygon; or the Editor.Select. Here's a where I'm at.

Could you take a look and let me know if anything looks off



So the code you provide works (when I have my feature layer in Mode="Selection" the only problems I'm running into is my FeatureDataGrid, doesn't open or get populate the first time I run the query. The features on the map are selected but nothing happens with the FeatureDataGrid. The second selection and every one after that works. I placed some break points and it basically doesn't get inside of MyEditor.EditCompleted += (s, ed) => so it never hits the if / foreach statement/loop on the first selection/query but it does go there on the second try and everyone after.

I also was sure what to do because I was getting an error message with the line MyEditor.EditCompleted += (s, e) => It was reporting that e was already declared in the parent and that I couldn't declare it again


    private void Editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            MyEditor.EditCompleted += (s, ed) =>   //Change e to ed
            {
                if (e.Action == Editor.EditAction.Select)
                {
                    foreach (var edit in e.Edits)
                    {
                        var g = edit.Graphic;
                        if (g.Selected)
                        {
                            var l = edit.Layer as FeatureLayer;
                            MyFeatureDataGrid.GraphicsLayer = l;

                        }
                    }
                }

              

                MyFeatureDataGridWindow.IsExpanded = true;
            };
        }



Here's my button click code, would you mind taking a look at how I'm activating MyEditor and how I moved the code from xaml to C#. I"m not sure I'm doing it right. A problem that I'm running into is if I do a simple query on a dynamic service so the first if statement is allowed if (Layers.SelectedIndex != 1) - the right draw mode happens and the layer is queried, a graphic layer is created and my featuregrid is populated but then the button becomes inactive and I can't use it again, I'm not sure why this is happening.

  private void SelectRectangle_Click(object sender, RoutedEventArgs e)
        {
          

            if (MyActiveLayer.SelectedIndex != 1)
            {
             //   SelectPoints.Command = MyEditor.CancelActive;
                SelectPoints.Command = MyEditor.ClearSelection;
                MyEditor.SelectionMode = DrawMode.None;

                MyDrawSurface.DrawMode = DrawMode.Rectangle;
                MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);
              
            }
            if (MyActiveLayer.SelectedIndex == 1)
            {
                Editor MyEditor = LayoutRoot.Resources["MyEditor"] as Editor;
                string[] myLayerIDs = { "BirdSightings" };
                MyEditor.LayerIDs = myLayerIDs;
                MyEditor.SelectionMode = DrawMode.Rectangle;
                MyEditor.ContinuousMode = false;
                SelectPoints.CommandParameter = SelectionMode.Multiple;
              //  SelectPoints.CommandParameter = MyEditor.Add;
                SelectPoints.Command = MyEditor.Select;
              //  SelectPoints.Command = new

            }

        }

0 Kudos
DavidAshton
Frequent Contributor
Jennifer,  I'm sorry to keep bothering you but I just can't seem to figure out why my button becomes inactive and I can' t use it after one use.

Jennifer thanks for all the replies! 

A problem that I'm running into is if I do a simple query on a dynamic service so the first if statement is allowed    if (Layers.SelectedIndex != 1) - the right draw mode happens and the layer is queried, a graphic layer is created and my featuregrid is populated but then the button becomes inactive and I can't use it again, I'm not sure why this is happening.


I'm also wondering how my change from e to ed effecting things.
MyEditor.EditCompleted += (s, ed) =>   //Change e to ed


Tha nks
Dave
0 Kudos
DavidAshton
Frequent Contributor
I was able to dig a little more into my problem about "if I do a simple query on a dynamic service so the first if statement is allowed if (Layers.SelectedIndex != 1) - the right draw mode happens and the layer is queried, a graphic layer is created and my featuregrid is populated but then the button becomes inactive and I can't use it again, I'm not sure why this is happening. "

So here's the code that gets ran when the button is clicked and it is not layer id = 1

 private void SelectPoints_Click(object sender, RoutedEventArgs e)
        {
            
                if (Layers.SelectedIndex != 1)
                {
                    //   SelectPoints.Command = MyEditor.CancelActive;
                    SelectPoints.Command = MyEditor.ClearSelection;
                    MyEditor.SelectionMode = DrawMode.None;

                    MyDrawSurface.DrawMode = DrawMode.Rectangle;
                    MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);
                    
                }



The user draws the selection rectangle and then
the feature data grid the selection_change event fires:

  private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fdg = sender as ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid;
            foreach (var item in e.AddedItems)
            {
                var g = GetGraphicSibling(item);


                MyAttachmentEditor.FeatureLayer = fdg.GraphicsLayer as FeatureLayer;
                MyAttachmentEditor.GraphicSource = g;


            }
        }





Now the button is disable

If I click on a record in the FeatureDataGrid the featuregrid selection change code fires

  private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fdg = sender as ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid;
            foreach (var item in e.AddedItems)
            {
                var g = GetGraphicSibling(item);


                MyAttachmentEditor.FeatureLayer = fdg.GraphicsLayer as FeatureLayer;
                MyAttachmentEditor.GraphicSource = g;


            }
        }





which fires this code

 private static Graphic GetGraphicSibling(object item)
        {
            if (item != null)
            {
                MethodInfo mi = item.GetType().GetMethod("GetGraphicSibling");
                if (mi != null)
                {
                    return mi.Invoke(item, null) as Graphic;

                }



            }
            return null;
        }


Now the button is active again.


If I click the button to do a new selection the code that fires is slightly different

first

 private void SelectPoints_Click(object sender, RoutedEventArgs e)
        {
            
                if (Layers.SelectedIndex != 1)
                {
                    //   SelectPoints.Command = MyEditor.CancelActive;
                    SelectPoints.Command = MyEditor.ClearSelection;
                    MyEditor.SelectionMode = DrawMode.None;

                    MyDrawSurface.DrawMode = DrawMode.Rectangle;
                    MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);
                    
                }


Then

  private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fdg = sender as ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid;
            foreach (var item in e.AddedItems)
            {
                var g = GetGraphicSibling(item);


                MyAttachmentEditor.FeatureLayer = fdg.GraphicsLayer as FeatureLayer;
                MyAttachmentEditor.GraphicSource = g;


            }
        }




Then

 private void MyEditor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            MyEditor.EditCompleted += (s, ed) =>   //Change e to ed
            {
                if (e.Action == Editor.EditAction.Select)
                {
                    foreach (var edit in e.Edits)
                    {
                        var g = edit.Graphic;
                        if (g.Selected)
                        {
                            var l = edit.Layer as FeatureLayer;
                            QueryDetailsDataGrid.GraphicsLayer = l;

                        }
                    }
                }
                ResultsDisplay.IsExpanded = true;
            };
        }



And now the user can draw the selection rectangle graphic...


Any ideas on how to break the process so I don't have to click in the table grid to fire the selection change to enable the button?
0 Kudos
JenniferNery
Esri Regular Contributor
I am a little bit confused with SelectPoints_Click. If you are using Editor.Select, you don't have to add a click event. The Select command will just work. You can try this SDK sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsExplicitSave. If you check Continuous Action check box, this sets Editor.ContinuousMode=True, which means any active command will remain active until you cancel it explicitly using CancelActive or another command gets activated. This should help you make continuous selection.
0 Kudos
DavidAshton
Frequent Contributor
Jennifer,

Thanks again for the reply.  Sorry for all the confusion.  This thread has become so long it is now confusing...thinking maybe I need a new one?


I am a little bit confused with SelectPoints_Click. If you are using Editor.Select, you don't have to add a click event. The Select command will just work. You can try this SDK sample: http://help.arcgis.com/en/webapi/sil...lsExplicitSave. If you check Continuous Action check box, this sets Editor.ContinuousMode=True, which means any active command will remain active until you cancel it explicitly using CancelActive or another command gets activated. This should help you make continuous selection.



So my original problem lies with the fact that I want to also use the spatial selection (http://help.arcgis.com/en/webapi/sil...m#SpatialQuery) tools and I don't want to have two tool bars.  One for selecting and editing feature layers and one for selecting map layers or graphics.  Using the stand alone editing tool (like the SDK exmaples - Editor.Select) will force me to have two toolbars or two selection tools so I'm trying to take the Editor.Select example and I'm trying to move the XAML to my code behind so I can have one toolbar.  A tool bar that can select map layers and a tool that can select feature layers and populate that to my featuredatagrid and then the enduser only see the commit button if it happened to be a editable feature layer.


 private void SelectPoints_Click(object sender, RoutedEventArgs e)
        {
            
                if (Layers.SelectedIndex != 1)
                {
                    //   SelectPoints.Command = MyEditor.CancelActive;
                    SelectPoints.Command = MyEditor.ClearSelection;
                    MyEditor.SelectionMode = DrawMode.None;
                   

                    MyDrawSurface.DrawMode = DrawMode.Rectangle;

                    MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);
                    
                }

                if (Layers.SelectedIndex == 1)
                {
                   
                    Editor MyEditor = LayoutRoot.Resources["MyEditor"] as Editor;
                    //Clear Selection Mode
                    MyEditor.SelectionMode = DrawMode.None;
                    string[] myLayerIDs = { "MYLINES" };
                    MyEditor.LayerIDs = myLayerIDs;
                    MyEditor.SelectionMode = DrawMode.Rectangle;
                    MyEditor.ContinuousMode = false;
                    SelectPoints.CommandParameter = SelectionMode.Multiple;
                    //  SelectPoints.CommandParameter = MyEditor.Add;
                    SelectPoints.Command = MyEditor.Select;
                    //  SelectPoints.Command = new

                }

              
            
        }
0 Kudos