I'm using MVVM, and I have a 3D view embedded in my WPF front end. I want to perform operations on this view such explode, unexplode, and home. I have functions (i.e. Explode(amount)
, Unexplode()
, Home()
) on the view that do these, but how can I communicate what I want to do from the view model?

- 9,642
- 10
- 71
- 141
-
See [my answer](http://stackoverflow.com/a/21026798/643085) of a similar question. --Edit:-- I just realized you asked that question too :P – Federico Berasategui Apr 07 '14 at 19:14
-
That was a long time ago. There are some proponents of MVVM, the loud ones, that say that you should never have code behind that is coupled to anything outside of the view. Having listened to them for such a long time, it physically hurts me to put this content into the code behind. But I guess that is the best thing to do. – Jordan Apr 07 '14 at 19:24
-
either that, or use a Messenger/EventAggregator (see MVVM Light's `Messenger` class or PRISM's `EventAggregator`), or create attached properties and put your logic in their `OnDependencyPropertyChanged(...)` – Federico Berasategui Apr 07 '14 at 19:26
-
Or you can use `Caliburn Micro`'s attached properties e.g, `Message.Attach`. – 123 456 789 0 Apr 07 '14 at 19:50
2 Answers
If you don't want third party framework, I would suggest using Behaviors
and do Interactions
in your control
.
If not like HighCore
said
see MVVM Light's Messenger class or PRISM's EventAggregator), or create attached properties and put your logic in their OnDependencyPropertyChanged(...)
Other than that I also suggest using Caliburn Micro
's Message.Attach
.

- 10,565
- 4
- 43
- 72
You could also wrap the 3D control or view around an interface say IMap3D that exposes these methods and then pass this interface to your view model.
When it comes to GIS/mapping I typically shy away from events. So while using EventAggregators or similar patterns will work, having direct access to the mapping object removes unnecessary layers of abstraction, which typically come in handy during those fine performance tuning exercises.
public interface IMap3D
{
void Explode(double amount);
void UnExplode();
void Home();
}
public class MapViewModel
{
private readonly IMap3D _map3D;
public MapViewModel(IMap3D map3D)
{
_map3D = map3D;
}
public ICommand ExplodeCommand
{
get
{
return new RelayCommand(args => true, o > { // put code to Explode _map3D here });
}
}
// expose commands for the other 3D view actions
}
Note that you may have to launch another UI element to extract the amount with which you want xplode the 3D view, unless you are exploding using a fixed amount. That can all be done as part of the command handling.

- 18,107
- 29
- 105
- 185
-
This is a good idea. I can then reverse bind it to a property on my view model. – Jordan Apr 09 '14 at 13:14