0
votes

I'm implementing the MVVM pattern in a Silverlight Application that utilizes 3rd party software (ESRI's Silverlight API). This 3rd party control only exists in the XAML:

<esri:Map x:Name="map" ... />

The ViewModel has an ObservableCollection of object which are bound to a ListBox. When a user double clicks on a specific item, the Model (which is bound to a user control inside that ListBox) fires an event which the "Main" view model is subscribed to.

My question is, in the "Main" ViewModel where the esri:Map resides, how do I call functions from this 3rd party control with data from the event (ex. map.ZoomTo( result ); )? The only solution I have so far is to move the event code (in the ListBox user controls) from the ViewModel into the Code-Behind and then subscribe to those events in the "MainPage" xaml and fire the code in the code behind.

Is this the best option utilizing the MVVM pattern??

2

2 Answers

1
votes

Sounds like you want either a Blend Trigger or Behavior, depending upon the exact scenario. Here's an introduction to them.

0
votes

That's a perfectly valid approach for "ViewModel First" MVVM. Another approach is to have the Code-Behind be ViewModel Aware, and directly call methods in the View Model from the event handlers - which is suitable for a "View First" approach to MVVM.

I am a huge fan of separating the whole thing, and using a good MVVM framework like Caliburn.Micro. With a framework like such, you could do something more OOPish, very SOLID, still MVVM, by utilizing a MEF:

  1. Your View would simply trigger events in an Event Aggregator, with a message payload with information to whatever receiver handles it. The View doesn't care who handles the event, all it cares about is to raise the event to the aggregator.

  2. Your View Models would handle whatever events are appropriate for each one. They don't care who raises the event, only that they are in charge of handling events from the Aggregator.

You will find that direct ViewModel first and View first implementations, through event handlers, of MVVM begin to grow cumbersome as your enterprise solition grows: it's scalability is limited.