Select to view content in your preferred language

Propblems with Editor using MVVM approach

1000
8
06-21-2010 01:24 PM
MikeKaufman
Emerging Contributor
I am having an issue getting a selection to show up properly on a map while having the Editor commands refresh correctly. I have created a ViewModel that has a Property exposing a LayerCollection.
Public Class LayerViewModel
    Private _layerCollection As New ESRI.ArcGIS.Client.LayerCollection
    Private WithEvents _selectionLayer As ESRI.ArcGIS.Client.FeatureLayer

    Sub New()
        CreateLayerCollection()
    End Sub

    Public ReadOnly Property ViewableLayers As ESRI.ArcGIS.Client.LayerCollection
        Get
            Return _layerCollection
        End Get
    End Property

    Public Sub ClearSelectionLayer()
        For Each feature In _selectionLayer.SelectedGraphics.ToList
            feature.UnSelect()
        Next
    End Sub

    Private Function CreateLayerCollection()

        _layerCollection.Add(New ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer With {
                .ID = "Parcels",
                .Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"})


        _selectionLayer = New ESRI.ArcGIS.Client.FeatureLayer With {
                .ID = "Selection",
                .Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/0",
                .Mode = ESRI.ArcGIS.Client.FeatureLayer.QueryMode.SelectionOnly}


        _layerCollection.Add(_selectionLayer)

        Return (_layerCollection)
    End Function

End Class

I bind the Map controlâ??s Layers Dependency Property to this my ViewModel property.
        <esri:Map x:Name="MyMap" Layers="{Binding ViewableLayers}"  Extent="-117.19034671783447,34.051488876211806,-117.16030597686768,34.07294654833095" >

        </esri:Map>
When I run the app and do a select no selected features are visible however the Remove and Clear buttons become enabled. When I uncomment the highlighted line of code below in my Views Code behind the selection shows up now but the Remove and Clear buttons do not enable. Can one explain why I am getting this behavior? I have modified the sample code from the Feature Layer Selection to include my new ViewModel approach http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection
Class MainWindow
    Public WithEvents MyEditor As New ESRI.ArcGIS.Client.Editor

    Private vm As New LayerViewModel

    Sub New()
        Resources.Add("MyEditor", MyEditor)
        Me.DataContext = vm

        ' This call is required by the designer.
        InitializeComponent()


        ' Add any initialization after the InitializeComponent() call.


        EditorToolStrip.DataContext = MyEditor
        MyEditor.Map = MyMap

    End Sub

    Private Sub MyEditor_EditCompleted(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Client.Editor.EditEventArgs) Handles MyEditor.EditCompleted
        vm.ClearSelectionLayer()
    End Sub
End Class
0 Kudos
8 Replies
dotMorten_esri
Esri Notable Contributor
Does it work if you set the mode to SnapShot?
0 Kudos
MikeKaufman
Emerging Contributor
Does it work if you set the mode to SnapShot?


No, it actually stops working completely.

I messed around a little more an found when I implement the code below on the EditCompleted event the editor buttons get the correct state and the selection is visible.  This however puts the graphics a selected state thus getting a the selected symbol.  And nothing is visible if only one graphic is selected.  It seems that commands behind remove and clear need at least one graphics selected to work in MVVM.

        For Each feature As Graphic In _selectionLayer.Graphics.ToList
            feature.UnSelect()
            feature.Select()
        Next
0 Kudos
dotMorten_esri
Esri Notable Contributor
Note that EditCompleted might fire a little more often than you think it will (like when the do a selection). Are you sure you always want to clear selection no matter what e.Action is?
0 Kudos
MikeKaufman
Emerging Contributor
Note that EditCompleted might fire a little more often than you think it will (like when the do a selection). Are you sure you always want to clear selection no matter what e.Action is?


That was just the first thing I tried.  I placed a button to fire the work around code now but it still does not resolve the odd behavior.  I will get a very simple solution demonstrating it and post the link as soon as I can.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Thanks. A sample to repro this would be nice. Btw. note that 2.0RC was released today, so make sure it's still an issue with that release.
0 Kudos
MikeKaufman
Emerging Contributor
OK here is the sample project demonstrating the odd behavior. http://cid-4cf0e82cc2054ef1.office.live.com/self.aspx/Public%20Code

There are 3 examples
1. An implemented MVVM pattern to generate the layers and do selection (top)
2.  A converted ESRI example to vb WPF  that works great (bottom left)
3.  A converted ESRI example to vb WPF  that removed the defined symbol from the sample (bottom right)

One thing that I am noticing in while doing my dev and this example is if I don't unselect the graphics I can't click to select them.  Just another little add thing I have noticed.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Thanks for the very nice repro.
The issue is that you never set a renderer on your FeatureLayer. Since you are hitting a 9.3.1 server, symbology is not automatically returned to the client (with v10.x not setting the renderer will default to the service's symbology).

In LayerViewModel.vb add:
_selectionLayer.Renderer = New SimpleRenderer() With {.Symbol = New SimpleMarkerSymbol()}

So bottom line is that as far as I can tell, everything is working as expected, but since there's no symbol to render your layer with, you won't see anything.
0 Kudos
MikeKaufman
Emerging Contributor
Thanks for the very nice repro.
The issue is that you never set a renderer on your FeatureLayer. Since you are hitting a 9.3.1 server, symbology is not automatically returned to the client (with v10.x not setting the renderer will default to the service's symbology).

In LayerViewModel.vb add:
_selectionLayer.Renderer = New SimpleRenderer() With {.Symbol = New SimpleMarkerSymbol()}

So bottom line is that as far as I can tell, everything is working as expected, but since there's no symbol to render your layer with, you won't see anything.


That works great to get my original issue solved dealing with not being able to see the returned graphics.  But now it has brought up another issue on how I might be going about selecting graphics.

When I set the Renderer the highlight color gets ignored.  As I move my code away from the work around I was using to the Renderer I lose my highlighted graphics color.  I am doing something very similar to the graphics selection example when selecting the states.  When the state graphics are return the graphics and the feature datagird highlighting is synchronized between the two.  I rely on knowing what the highlighted items are in order to do that same synchronization elsewhere in my app.  I also need to see what is highlighted.

I think I am just going to have to go back to square one and really understand the differences and benefits of using the editor for selection or a query task.  Right now I think I will need to use query tasks so I can gain more control over the behavior.  Thanks for you help 🙂
0 Kudos