Select to view content in your preferred language

Unable to save edits to graphics on GraphicsLayer using Editor control

957
6
03-16-2011 12:02 PM
AndyWright
Frequent Contributor
I took the sample from here ...

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsAutoSave

... and modified it to use a GraphicsLayer instead of a FeatureLayer.  I added buttons to enable the addition of points, lines, and polygons using a Draw object.  I am able to add them, edit their vertices, and move them around no problem.  But the only changes that persist are the ones to the points.  Every time I move or edit the vertices of a line or polygon and then click the New Selection button the graphic reverts back to its original state prior to making edits.

I noticed with the FeatureLayer there is an explicit Update method that can be called, but I don't see anything similar with a GraphicsLayer.  How can I explicitly save edits to graphics on a graphics layer so they will persist across a given edit session?

Here's my XAML ...


<UserControl x:Class="ESRIEditingTest.MainPage"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:esri="http://schemas.esri.com/arcgis/client/2009" 
 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
 xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
 xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
 xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <esri:Editor x:Key="MyEditor" EditCompleted="Editor_EditCompleted"
                         Map="{Binding ElementName=MyMap}" 
                         LayerIDs="ThreatAreas"
                         GeometryServiceUrl="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer" />
        </Grid.Resources>

        <esri:Map x:Name="MyMap" Extent="-13054165,3850112,-13027133,3863559" esri:Editor.SnapDistance="20" esri:Editor.SnapKey="S">
            <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
            <esri:GraphicsLayer ID="ThreatAreas" />
           <!--<esri:FeatureLayer ID="ThreatAreas"  
                               Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/FeatureServer/2"                  
                               AutoSave="True" 
                               Mode="OnDemand" 
                               ValidateEdits="True" 
                               DisableClientCaching="True" />-->
        </esri:Map>

        <Border Background="#FF919191" BorderThickness="1" CornerRadius="5"
             HorizontalAlignment="Right"  VerticalAlignment="Top"
             Padding="5" BorderBrush="Black" Margin="0,5,5,0"
                DataContext="{StaticResource MyEditor}">

            <StackPanel x:Name="EditorTools" 
                        Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="AddButton" Margin="2"
                         ToolTipService.ToolTip="Civil Disturbance"
                         Content="Add Polygon" 
                         Command="{Binding Add}">
                        <Button.CommandParameter>
                            <sys:Int32>10101</sys:Int32>
                        </Button.CommandParameter>
                    </Button>
                    <Button x:Name="SelectButton" Margin="2"                            
                            Command="{Binding Select}" 
                            CommandParameter="New" >
                        <TextBlock>New<LineBreak/>Selection</TextBlock>
                    </Button>
                    <Button x:Name="AddSelectButton" Margin="2"                            
                            Command="{Binding Select}" 
                            CommandParameter="Add">
                        <TextBlock>Add to<LineBreak/>Selection</TextBlock>
                    </Button>
                    <Button x:Name="RemoveSelectButton" Margin="2"                             
                            Command="{Binding Select}"
                            CommandParameter="Remove" >
                        <TextBlock>Remove from<LineBreak/>Selection</TextBlock>
                    </Button>
                    <Button x:Name="ClearSelectionButton" Margin="2"                            
                            Command="{Binding ClearSelection}">
                        <TextBlock>Clear<LineBreak/>Selection</TextBlock>
                    </Button>
                    <Button x:Name="DeleteButton" Margin="2"                           
                            Command="{Binding DeleteSelected}">
                        <TextBlock>Delete<LineBreak/>Selected</TextBlock>
                    </Button>
                    <Button x:Name="CancelButton" Margin="2"                           
                            Command="{Binding CancelActive}">
                        <TextBlock>Cancel<LineBreak/>Action</TextBlock>
                    </Button>
                    <Button x:Name="AddPolyButton" Margin="2" Click="AddPolyButton_Click">
                        <TextBlock>Add<LineBreak/>Poly</TextBlock>
                    </Button>
                    <Button x:Name="AddPointButton" Margin="2" Click="AddPointButton_Click">
                        <TextBlock>Add<LineBreak/>Point</TextBlock>
                    </Button>
                    <Button x:Name="AddLineButton" Margin="2" Click="AddLineButton_Click">
                        <TextBlock>Add<LineBreak/>Line</TextBlock>
                    </Button>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Button x:Name="EditVerticesButton" Margin="2"                        
                         Command="{Binding EditVertices}">
                        <TextBlock>Edit Vertices<LineBreak/>/Move Selected</TextBlock>
                    </Button>
                    <Button x:Name="CutButton" Margin="2"                            
                            Command="{Binding Cut}">
                        <TextBlock>Cut<LineBreak/>Selected</TextBlock>
                    </Button>
                    <Button x:Name="ReshapeButton" Margin="2"                           
                            Command="{Binding Reshape}">
                        <TextBlock>Reshape<LineBreak/>Selected</TextBlock>
                    </Button>
                    <Button x:Name="UnionButton" Margin="2"                           
                            Command="{Binding Union}">
                        <TextBlock>Union<LineBreak/>Selected</TextBlock>
                    </Button>
                    <Button x:Name="SaveButton" Margin="2"                           
                            Command="{Binding Save}">
                        <TextBlock>Save</TextBlock>
                    </Button>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <CheckBox x:Name="FreehandCheckBox" VerticalAlignment="Center" Margin="2"
                          IsChecked="{Binding Path=Freehand, Mode=TwoWay}" 
                          Content="Freehand Draw" />
                    <CheckBox x:Name="AutoCompleteCheckBox" VerticalAlignment="Center" Margin="2"
                          IsChecked="{Binding Path=AutoComplete, Mode=TwoWay}" 
                          Content="Auto Complete" />
                    <CheckBox x:Name="AutoSelectCheckBox" VerticalAlignment="Center" Margin="2"
                          IsChecked="{Binding Path=AutoSelect, Mode=TwoWay}" 
                          Content="Auto Select" />
                    <CheckBox x:Name="ContinuousCheckBox" VerticalAlignment="Center" Margin="2"
                          IsChecked="{Binding Path=ContinuousMode, Mode=TwoWay}" 
                          Content="Continuous Action" />
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>

0 Kudos
6 Replies
AndyWright
Frequent Contributor
And here's my code behind ...


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;

namespace ESRIEditingTest
{
    public partial class MainPage : UserControl
    {
        private Draw _drawObject = new Draw();

        public MainPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _drawObject.Map = MyMap;
            _drawObject.DrawComplete += new EventHandler<DrawEventArgs>(_drawObject_DrawComplete);
        }

        void _drawObject_DrawComplete(object sender, DrawEventArgs e)
        {
            GraphicsLayer gl = MyMap.Layers["ThreatAreas"] as GraphicsLayer;
            Graphic gr = new Graphic();

            if (e.Geometry is MapPoint)
            {
                SimpleMarkerSymbol sms = new SimpleMarkerSymbol();
                sms.Size = 10;
                sms.Color = new SolidColorBrush(Colors.Red);
                sms.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;

                MapPoint p = e.Geometry as MapPoint;
                gr = new Graphic
                {
                    Geometry = p,
                    Symbol = sms
                };
            }
            else if (e.Geometry is Polyline)
            {
                SimpleLineSymbol sls = new SimpleLineSymbol();
                sls.Color = new SolidColorBrush(Colors.Red);
                sls.Style = SimpleLineSymbol.LineStyle.Solid;
                sls.Width = 2;

                Polyline p = e.Geometry as Polyline;
                gr = new Graphic
                {
                    Geometry = p,
                    Symbol = sls
                };
            }
            else if (e.Geometry is Polygon)
            {
                SimpleFillSymbol sfs = new SimpleFillSymbol();
                sfs.BorderBrush = new SolidColorBrush(Colors.Cyan);
                sfs.BorderThickness = 2;
                sfs.Fill = new SolidColorBrush(Colors.Red);
                sfs.Fill.Opacity = 0.7;

                Polygon p = e.Geometry as Polygon;
                gr = new Graphic
                {
                    Geometry = p,
                    Symbol = sfs
                };
            }

            gl.Graphics.Add(gr);

            _drawObject.IsEnabled = false;
        }

        private void AddPolyButton_Click(object sender, RoutedEventArgs e)
        {
            _drawObject.IsEnabled = true;
            _drawObject.DrawMode = DrawMode.Polygon;
        }

        private void AddPointButton_Click(object sender, RoutedEventArgs e)
        {
            _drawObject.IsEnabled = true;
            _drawObject.DrawMode = DrawMode.Point;
        }

        private void AddLineButton_Click(object sender, RoutedEventArgs e)
        {
            _drawObject.IsEnabled = true;
            _drawObject.DrawMode = DrawMode.Polyline;
        }

        private void Editor_EditCompleted(object sender, Editor.EditEventArgs e)
        {

        }
    }
}

0 Kudos
JenniferNery
Esri Regular Contributor
This is the intended behavior. When you activate another command before completing the edit, the uncommitted geometry change will be lost. To complete the edit, you click on the geometry being edited or click on another feature.

Note also that moving a point, unlike moving/editing polyline/polygon/envelope geometries is committed as soon as the geometry is moved.
0 Kudos
AndyWright
Frequent Contributor
Ahhh!  I didn't notice that yesterday.  Thanks. 

Is there any way to explicitly save the edit like when the EditComplete event fires or something or do they only get saved the two ways you explained?
0 Kudos
JenniferNery
Esri Regular Contributor
These are the only two ways to commit the edit on the geometry.
0 Kudos
AndyWright
Frequent Contributor
Ok, thanks Jennifer ...
0 Kudos
GéraldReusser
Emerging Contributor
This is the intended behavior. When you activate another command before completing the edit, the uncommitted geometry change will be lost. To complete the edit, you click on the geometry being edited or click on another feature.

Note also that moving a point, unlike moving/editing polyline/polygon/envelope geometries is committed as soon as the geometry is moved.


Hello. I don't unterstand the second way. I've tryed to click on another graphic of the same featurelayer, but it doesn't work (the modified graphic comes back to original form). What do you mean  with this alternative ?

thanks.
0 Kudos