Using SketchEditor in MVVM

11-10-2020 05:16 AM
Esri Regular Contributor

Hi all

I have a program in runtime that uses MVVM.

I would like to use SketchEditor in one of the commands to draw a line on screen.

The map is send to the model but not the MapView.

I found that in the xml I can add SketchEditor="{binding Draw}" but I am not sure how to use it.

Anybody have a code sample?


0 Kudos
2 Replies
Esri Regular Contributor

There may be a few SketchEditor commands that you can specify in XAML (like Complete/Cancel/Undo/Redo/Delete). However, you can only begin a draw in code-behind using one of the StartAsync methods. Based on SketchCreation mode, shape is drawn based on MapView events. For example, tap/click for point or adding vertices in polyline/polygon, drag/move for freehand, arrow, rectangle, triangle, circle, ellipse shapes.

Few XAML example code would be to set DataContext to MapView's SketchEditor and use Command Binding.

            <Grid DataContext="{Binding ElementName=MyMapView,Path=SketchEditor}">
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <ColumnDefinition />
                    <ColumnDefinition />
                <CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}"
                      Content="IsEnabled" />
                <CheckBox IsChecked="{Binding IsVisible, Mode=TwoWay}"
                          Grid.Column="1" />
                <Slider Minimum="0"
                        Value="{Binding Opacity, Mode=TwoWay}"
                        Grid.Row="1" />
                <Button x:Name="AddButton"
                        Command="{Binding AddCommand}"
                        Grid.Row="2" />
                <Button Content="Delete"
                        Command="{Binding DeleteCommand}"
                        Grid.Column="1" />
                <Button Content="Undo"
                        Command="{Binding UndoCommand}"
                        Grid.Row="3" />
                <Button Content="Redo"
                        Command="{Binding RedoCommand}"
                        Grid.Column="1" />
                <Button Content="Cancel"
                        Command="{Binding CancelCommand}"
                        Grid.Row="4" />
                <Button Content="Complete"
                        Command="{Binding CompleteCommand}"
                        Grid.Column="1" />

If you have provided SketchEditorConfiguration in resource, you can access this by key and update it's properties using XAML-binding too.

        <esri:SketchEditConfiguration x:Key="MySketchEditConfiguration" />

            <StackPanel DataContext="{StaticResource MySketchEditConfiguration}">
                <ComboBox x:Name="ResizeModes"
                          SelectedItem="{Binding ResizeMode, Mode=TwoWay}" />
                <CheckBox IsChecked="{Binding RequireSelectionBeforeDrag, Mode=TwoWay}"
                          Content="RequireSelectionBeforeDrag" />
                <CheckBox IsChecked="{Binding AllowMove, Mode=TwoWay}"
                          Content="AllowMove" />
                <ComboBox x:Name="VertexEditModes"
                          SelectedItem="{Binding VertexEditMode, Mode=TwoWay}" />
                <CheckBox IsChecked="{Binding AllowVertexEditing, Mode=TwoWay}"
                          Content="AllowVertexEditing" />
                <CheckBox IsChecked="{Binding AllowRotate, Mode=TwoWay}"
                          Content="AllowRotate" />

Depending whether you are creating a new shape or editing an existing geometry, you may call any of the StartAsync methods.

var editConfig = this.Resources["MySketchEditConfiguration"] as SketchEditConfiguration;
var geometry = await MyMapView.SketchEditor.StartAsync(creationMode, editConfig);
0 Kudos
MVP Regular Contributor

I use a different approach and is based on the idea that one may want to manage drawing in a view other than the main view.  I believe the approach from @JenniferNery would only work in the view model tied to the view that contains MapView.  There is also an assumption that a library which provides event aggregation is being used (e.g., MVVMLight although I use Prism).  One might argue is more complex, but I believe it offers a way to provide a true MVVM solution in a complex application that can go beyond a single view.

I have a custom MapView control, which has it's own ViewModel.  My Xaml (with only the relevant pieces) looks like this


<esri:MapView xmlns=""

             Map="{Binding Map}"
                   SketchEditor="{Binding SketchEditor}"
                   GraphicsOverlays="{Binding GraphicsOverlays}"               


In the supporting ViewModel I inject the IEventAggregator, also one thing I found is that the bound SketchEditor needs to be initialized in the constructor .  


public MapViewControlViewModel(IEventAggregator eventAggregator, ...)

	EventAggregator = eventAggregator;
	SketchEditor = new SketchEditor();


The idea now is that the SketchEditor will be sent out to the rest of the application by publishing an event.  There is one other caveat, and that is we can not send off our SketchEditor until the map in initialized.

So we need a couple events.  One event that can be published to the application to indicate the Map is loaded (MapLoadedEvent), and another that can send the SketchEditor out (SketchEditorInitializedEvent).  We put a handler for our MapLoaded event in the MapViewControlViewModel and then in our handler publish out the SketchEditorInitializedEvent


private void OnMapLoaded(MapLoadedEventArgs obj)
		Map = obj.Map;
	catch (Exception e)
		Log.Error(e, e.Message);


Now anywhere I need the SketchEditor I can handle this event


protected EditBaseViewModel(IEventAggregator eventAggregator...) : base(eventAggregator)
	SubscriptionTokens.Add(EventAggregator.GetEvent<SketchEditorInitializedEvent>().Subscribe(se => SketchEditor = se));     


This will then support a modular application with the ability to draw from any ViewModel that provides this functionality


0 Kudos