Select to view content in your preferred language

Attachment Editor with FeatureDataGrid_SelectionChange event

3342
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
JenniferNery
Esri Regular Contributor
0 Kudos
DavidAshton
Frequent Contributor
Jennifer,

Thanks for the reply and the reference but I'm still lost.  Should I just drop the following statements somewhere?? I can't seem to grasp it.  THanks
MyAttachmentEditor.FeatureLayer = g.Layer as FeatureLayer;
                        MyAttachmentEditor.GraphicSource = g;

  private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            foreach (var item in e.AddedItems)
            {
               var g = GetGraphicSibling(item);
                if (g.Layer is FeatureLayer)
                {
                    
                        MyAttachmentEditor.FeatureLayer = g.Layer as FeatureLayer;
                        MyAttachmentEditor.GraphicSource = g;
                   
                }
            }
        }

        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;
        }
0 Kudos
JenniferNery
Esri Regular Contributor
Yes this code should replace your FeatureDataGrid_SelectionChange event handler. This is how you can get the graphic from FDG.row. Once you have the graphic, you can set AttachmentEditor.GraphicSource property.
0 Kudos
DavidAshton
Frequent Contributor
Yes this code should replace your FeatureDataGrid_SelectionChange event handler. This is how you can get the graphic from FDG.row. Once you have the graphic, you can set AttachmentEditor.GraphicSource property.


Jennifer Thanks for the reply.  I couldn't get it working so I step away for awhile (sometimes that works, this time it didn't :rolleyes:) In my code above I thought I was setting the AttachmentEditor.GraphicSource property with the lines :
MyAttachmentEditor.FeatureLayer = g.Layer as FeatureLayer;
                        MyAttachmentEditor.GraphicSource = g;


But nothing ever happens when I change a selection in the FeatureDataGrid.  Am I missing it?

Thnaks
David
0 Kudos
JenniferNery
Esri Regular Contributor
Try debugging to your code to check if AttachmentEditor.FeatureLayer and AttachmentLayer.GraphicsSource get set properly. You can also check if the FeatureLayer..LayerInfo.HasAttachments is True. It could be that the service does not support attachments.
0 Kudos
DavidAshton
Frequent Contributor
Jennifer,

Thanks for sticking with me! I appreciate it!

Try debugging to your code to check if AttachmentEditor.FeatureLayer and AttachmentLayer.GraphicsSource get set properly. You can also check if the FeatureLayer..LayerInfo.HasAttachments is True. It could be that the service does not support attachments.



I've debugged several times and I'm not getting any error messages and my code runs. I keep thinking that I'm not setting the AttachmentEditor.FeatureLayer and AttachmentLayer.GraphicsSource correctly. I do have attachments on my lines, I can view the attachments and add/delete attachments so they are working; here is the attachment editor from the Interactive SDK that works (see code below)

    void EditorWidget_EditCompleted(object sender, Editor.EditEventArgs e)
        {

            if (e.Action == Editor.EditAction.ClearSelection) //|| e.Action == Editor.EditAction.Select
            {
                MyAttachmentEditor.GraphicSource = null;

            }

            if (e.Action == Editor.EditAction.Add || e.Action == Editor.EditAction.Select)
            {
                foreach (var edit in e.Edits)
                {
                    if (edit.Layer is FeatureLayer)
                    {
                        if (e.Action == Editor.EditAction.Add || edit.Graphic.Selected)
                        {
                            MyAttachmentEditor.FeatureLayer = edit.Layer as FeatureLayer;
                            MyAttachmentEditor.GraphicSource = edit.Graphic;
                            
                        }
                    }
                }
            }
        }




I am having trouble figuring out how or if I'm setting the AttachmentEditor.FeatureLayer and AttachmentLayer.GraphicsSource properly. I don't believe I am but I'm not sure what to try...I've switch it so many times. Should I be setting AttachmentEditor.FeatureLayer and AttachmentLayer.GraphicsSource to g, which is already a graphic or item?? How can I check that?

 foreach (var item in e.AddedItems)
            {
               var g = GetGraphicSibling(item);
                if (g.Layer is FeatureLayer)
                {
                    
                        MyAttachmentEditor.FeatureLayer = g.Layer as FeatureLayer;
                        MyAttachmentEditor.GraphicSource = g;
                   
                }
            }


Thanks
Dave
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Your code might not work when removing graphics from the selection.
Try something like:

private void FeatureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var fdg = sender as ESRI.ArcGIS.Client.Toolkit.FeatureDataGrid;
    var g = e.AddedItems.Cast<object>().Select(GetGraphicSibling).FirstOrDefault() ??
            fdg.SelectedGraphics.Where(s => !e.RemovedItems.Cast<object>().Select(GetGraphicSibling).Contains(s)).FirstOrDefault();
    MyAttachmentEditor.FeatureLayer = fdg.GraphicsLayer as FeatureLayer;
    MyAttachmentEditor.GraphicSource = g;
}
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;
}
0 Kudos
DavidAshton
Frequent Contributor
Dominique,

I placed you code in application and initially it didn't work but then I created a new selection button using the example from
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection
and it works.  Next I modify the code I was originally trying to use and got it to work also (see below). 

I guess my problem is, I am want to use the spatial selection (http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery) tools and not this tool that I just created that only works on one specific feature layer.  I tried to fuse the two and did some searching and couldn't find a solution.  It seems like my problem with the spatial selection tools is I am setting the featuredatagrid.Graphicslayer to a graphics layer and not feature layer because of how the spatial selection tool works. 

I keep coming back to writing a simple if that will check if MyActiveLayer is a featurelayer and if so (per you suggestion) on this post http://forums.arcgis.com/threads/47996-Feature-Data-Grid-Editing
set MyFeatureDataGrid.GraphicsLayer = MyActiveLayer;  but my MyActiveLayer is a graphic layer even if it is based off a Editable FeatureLayer it doesn't work...hope you can follow this.

I keep trying this but this doesn't work either.

 
{
                if (MyActiveLayer.SelectedIndex == 2 || (MyActiveLayers.SelectedIndex == 3) // Layer is none Editable Feature Layer, without attachments
                {
                    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;
                        }
                        MyFeatureDataGrid.GraphicsLayer = graphicsLayer;
                        graphicsLayer.Graphics.Insert(0, feature);

                    }

                }


                if ((MyActiveLayers.SelectedIndex == 0 || (MyActiveLayers.SelectedIndex == 1) // Layer is a Editable Feature Layer, has attachments
                {
                    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;

                    }

                }





My modified FeatureDataGrid_Selection Change that works with the feature layer selection button from SDK (http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection)

  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);
                if (g.Layer is FeatureLayer)
                {
                    
                       MyAttachmentEditor.FeatureLayer = fdg.GraphicsLayer as FeatureLayer;
                        MyAttachmentEditor.GraphicSource = g;
                   
                }
            }
        }

        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;
        }
0 Kudos
JenniferNery
Esri Regular 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.
0 Kudos