artganify
artganify

Reputation: 703

Calling method on control in MVVM

I'm currently working with the ArcGIS Runtime SDK .NET where I'd like to have the current viewport of the map bound to a property on my view model. However, the view port (expressed as Extent) does not have a setter, despite being a dependency property.

In order to set the viewport, I need to call a method on the control. But how do I do that from the view model? I already found similiar questions here on SO, but most of them were answered with The view model shouldn't be aware of the view. I agree with that, but unfortunately I can't change the fact that the setter of a property on a proprietary control is a different method than the actual property I can bind to and read values from.

Upvotes: 0

Views: 472

Answers (2)

dotMorten
dotMorten

Reputation: 1966

You can use an attached property to push make a VM request any listening view to go to a certain viewpoint. I'm using that in my sample here:

https://github.com/Esri/arcgis-runtime-demos-dotnet/blob/master/src/TurnByTurn/RoutingSample.Shared/CommandBinder.cs

In your VM you simply raise INPC for a viewpoint:

public Viewpoint ViewpointRequested
{
    get { return m_ViewpointRequested; }
    private set
    {
        m_ViewpointRequested = value;
        RaisePropertyChanged("ViewpointRequested");
    }
}

And then lastly just bind this to the attached property on the MapView:

<esri:MapView Map="{Binding Map}"
             local:CommandBinder.RequestView="{Binding ViewpointRequested}" />

Upvotes: 0

Emad
Emad

Reputation: 3929

There are many ways to do the job that might be considered MVVM friendly. The one that I suggest is that you wrap your ArcGIS view in a custom control that you have full control over. This way you can expose your required dependency properties and handle their setters in your custom control.

I use this method almost every time I'm using a third-party component this way I make the component loosely coupled with my other code and I can replace them easily.

Upvotes: 3

Related Questions