Select to view content in your preferred language

Undo edits of the EditorWidget

3080
12
08-05-2010 01:20 AM
MannusEtten
Emerging Contributor
the editor widget is a cool control to easy add new features and fill in the attributes. So we want to use this control. Our requirement to fulfill is to check the new feature after drawing.

The new graphic must be checked if it intersects with a certain other shape. We created a webservice for this functionality. So the check is no problem. But if the check say "editing on this place is not allowed" I would like to cancel the edit.

I implemented logic in the EditorWidget.Edit_Completed and FeatureLayer_BeginEditing event handlers. These handlers do have a input parameter EditAction. In both event handlers I set the EditAction to "Cancel". But still my features are drawn and saved to the geodatabase.

So how can I cancel the edit operation of the EditorWidget? Why does the Editor-class has an "undo" and the EditorWidget doesn't?
0 Kudos
12 Replies
PaulLeedham
Deactivated User
We also liked the edit widget--it is a great control but we found similar limitations on controling the events/actions fired when clicking a tool.  Our solution was creating our own tool menu by building custom buttons to replicate the same functionality as the widget (tapping in to the editor class).  This allowed us to have more control over what each button does and would be a method to resolve your issue.

I hope this helps...

Paul Leedham
City of Hudson
http://gis.hudson.oh.us/
0 Kudos
JenniferNery
Esri Regular Contributor
You can set the layer's AutoSave property to false. Get the Editors from the EditorWidget's and TemplatePicker's DataContext. Note that these two editors are separate, you can get the TemplatePicker using Expression Blend "Edit Template" on EditorWidget. When you have both editors, you can listen to their EditCompleted event, perform the necessary checks for the graphics and call the save here. Kindly see the code below for guide.

        public MainPage()
        {
            InitializeComponent();
            Editor editorForWidget = this.MyEditorWidget.DataContext as Editor;            
            if(editorForWidget != null)
                editorForWidget.EditCompleted += new EventHandler<Editor.EditEventArgs>(editorForWidget_EditCompleted);
        }

        private void editorForWidget_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            foreach (var edits in e.Edits)
            {
                e.Action // check action (EditVertices, Reshape, Cut, Select,..)
                edits.Graphic // perform the check for graphic
                // if check succeeds, save the edits
                if (edits.Layer is FeatureLayer)
                    (edits.Layer as FeatureLayer).SaveEdits();
            }
        }


Jennifer
0 Kudos
MannusEtten
Emerging Contributor
I implemented the code given by ESRI Inc. Now I can detect and determine when to use SaveEdits for a featurelayer. But if I don't call this method, still the features are saved to the geodatabase.
So again? How can I undo an edit in a decent way?

My own implementation removed the feature after it was added...

            
Editor editor = editorWidget1.DataContext as Editor;
            if (editor != null)
            {
                editor.EditCompleted += new EventHandler<Editor.EditEventArgs>(editor_EditCompleted);
            }

        void editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            foreach (var edit in e.Edits)
            {
//                if(e.Action== Editor.EditAction.Save)
//                {
                    FeatureLayer featurerLayer = edit.Layer as FeatureLayer;
                    if (featurerLayer != null)
                    {
                        if (IsEditAllowed(edit.Graphic, featurerLayer.ID))
                        {
                            featurerLayer.SaveEdits();
                        }
                        featurerLayer.Update();
                    }
                //                }
            }
        }

        private bool IsEditAllowed(Graphic graphic, string layerName)
        {
            return layerName.Equals("punten");
        }


the xaml-code
        <esri:EditorWidget HorizontalAlignment="Left" Margin="391,298,0,0" Name="editorWidget1" VerticalAlignment="Top" Height="162" Width="302" AutoComplete="True" AutoSelect="True" Continuous="False" Freehand="True" GeometryServiceUrl="http://localhost/ArcGIS/rest/services/Geometry/GeometryServer"  Map="{Binding ElementName=Map}" ShowAttributesOnAdd="True" />
        <esri:Map x:Name="Map" Background="White" Margin="-32,-45,32,45">
         <esri:ArcGISDynamicMapServiceLayer ID="BaseLayer" Url="http://localhost:80/arcgis/rest/services/DBK/MapServer" />
            <esri:FeatureLayer ID="punten" Url="http://localhost/ArcGIS/rest/services/DBK/FeatureServer/0" OutFields="POST" AutoSave="False"/>
            <esri:FeatureLayer ID="lijnen" Url="http://localhost/ArcGIS/rest/services/DBK/FeatureServer/1" OutFields="POST" AutoSave="False"/>
        </esri:Map>
0 Kudos
JenniferNery
Esri Regular Contributor

The new graphic must be checked if it intersects with a certain other shape. We created a webservice for this functionality. So the check is no problem. But if the check say "editing on this place is not allowed" I would like to cancel the edit.


Do you perform this check prior to calling SaveEdits? If the issue is with adding new graphics, I assume you use the TemplatePicker within the EditorWidget. You need to get this from EditorWidgetStyle (use Expression Blend "EditTemplate"). Get the Editors from their DataContext and on the EditCompleted event, check that when an Add occurs or EditVertices occurs, they intersect the shape (as you mentioned in your requirements), if the new graphic fails this test, do not call SaveEdits.

Jennifer
0 Kudos
MannusEtten
Emerging Contributor
I use the editorWidget. Because this is the most easy to implement control for editing. Our customer can see which kind of feature he will add. The editing tools for edit vertices are handy too. Apparently this control is contains two controls. A TemplatePicker and something else?

But I took the editor from the datacontext of the editorwidget. Apparently this is the wrong datacontext. Can you give me a code sample how to grab the correct editor?

I connect now an event to the datacontext of the editorwidget. Is this editor in use for the toolbar which is shown by the EditorWidget?

How could I have known this without your help?
0 Kudos
JenniferNery
Esri Regular Contributor
The Editor from the EditorWidget, acts on the toolbar. The EditorWidget contains a TemplatePicker. If you have Expression Blend, you can see what UIElements are contained in the widget by adding an EditorWidget to an empty SL application, right-click on the widget and "Edit Template". You will find that just as in this example, the buttons contained in the widget are made active through an Editor:http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave

Jennifer
0 Kudos
MannusEtten
Emerging Contributor
I do not completely understand your reply. I dont have blend in use so maybe that is the reason. But if I take your first reply with the datacontext I get apparently the wrong one. So this EditorWidget has two datacontexts in use?
0 Kudos
JenniferNery
Esri Regular Contributor
The DataContext you retrieved from the EditorWidget is correct too. This is the Editor that acts on the toolbar so if you wish to check that EditGeometry/Select/Cut/Reshape/Union were performed, you will use the Editor from the widget.

The other sets of Editors come from the TemplatePicker. You can use this if you wish to monitor the Add.

The attached XAML code was produced by Expression Blend. You can apply this style to your EditorWidget, it is the DefaultStyle - I only added handler for TemplatePicker's Loaded event.

In the code-behind you can do the following:
 private void TemplatePicker_Loaded(object sender, RoutedEventArgs e)
        {
            TemplatePicker picker = sender as TemplatePicker;
            foreach (var t in picker.Templates)
            {
                t.Editor.EditCompleted += new EventHandler<Editor.EditEventArgs>(Editor_EditCompleted);
            }
        }


You can use the same event handler as the Editor from the widget so all are checked in one place.

I'm sorry if I was unclear with my previous posts. I thought you had ExpressionBlend.

Jennifer
0 Kudos
MannusEtten
Emerging Contributor
I will add this style to my xaml-file without the markup such a colors. Only the part with the buttons, tooltips and the template picker.

Hopefully I can control now the adds and do the saveedits only when I want that to do.

Besides this, what happens if I add an editor to the staticresources and use this for the datacontext of my editorWidget?
0 Kudos