views:

927

answers:

4

Hi, I have quite simple (I hope :)) problem:

In MVVM, View usually listens on changes of ViewModel's properties. However, I would sometimes like to listen on event, so that, for example, View could start animation, or close window, when VM signals.

Doing it via bool property with NotifyPropertyChanged (and starting animation only when it changes from false to true) is possible, but it feels like a hack, I'd much prefer to expose event, as it is semantically correct.

Also, I'd like to do it without code in codebehind, as doing viewModel.myEvent += handler there would mean that I'd have manually unregister the event in order to allow View to be GC'd - WPF Views are already able to listen on properties 'weakly', and I'd much prefer to program only declaratively in View.

The standard strong event subscription is also bad, because I need to switch multiple ViewModels for one View (because creating View every time takes too much CPU time).

Thank you for ideas (if there is a standard solution, a link to msdn will suffice)!

A: 

Some comments:

  • You can use the weak event pattern to ensure that the view can be GC'd even if it is still attached to the view model's event
  • If you're already switching multiple VMs in for the one view, wouldn't that be the ideal place to attach/detach the handler?
  • Depending on your exact scenario, you could just have the VM expose a state property which the view uses as a trigger for animations, transitions, and other visual changes. Visual state manager is great for this kind of thing.

HTH,
Kent

Kent Boogaart
Thanks for the links, Kent. I know about weak event pattern, but I hoped that WPF has something built in (to bind some action to VM event) + weak events are really tricky in c#: http://www.codeproject.com/KB/cs/WeakEvents.aspx .Switching might occur from several places, and I can't say when to unregister the last listener (this would be solved by weak events).State is what I don't need - I practically have one state, my events don't occur on state changes.Thank you, but I'll see if someone provides answer for my case.
Tomáš Kafka
By the way, I find it strange that you can trigger for example animation when bound property changes from x to y, but more natural scenario (trigger animation when event occurs) seems to be not supported...
Tomáš Kafka
The problem is that your model only got one state. Why do you want to start an animation if nothing changes in the model?
adrianm
Well, just imagine that you are building a twitter client, and you want to flash some icon when new messages arrive. What would be the state? The moment when messages arrived can signal an event, but it doesn't toggle state (if you switched from 'original state' to 'new messages arrived', how would you signal when another batch of messages arrive?). The semantics of this are event signal, not a state toggle, and I hoped WPF would respect this...
Tomáš Kafka
A: 

Like adrianm said, when you trigger your animation off a bool property you are actually responding to an event. Specifically the event PropertyChanged which the WPF subsystem. Which is designed to attach/detach correctly to/from so that you don't leak memory (you may forget to do this when wiring an event yourself and cause a memory leak by having a reference active to an object which otherwise should be GCed).

This allows you to expose your ViewModel as the DataContext for the control and respond correctly to the changing of properties on the datacontext through databinding.

MVVM is a pattern that works particularly well with WPF because of all these things that WPF gives you, and triggering off a property change is actually an elegant way to use the entire WPF subsystem to accomplish your goals :)

Jim Wallace
Hi Jim, thanks - I love MVVM too, however the problem is that when my property is false (or true) all the time, the PropertyChanged("MyPropertyName") has no effect - the listening wpf control is called, but it notices that no parameter really changed, so there is no reason to (for example) start the animation.I'd have to make an ugly hack like MyProp = true; PropertyChanged("MyProp"); MyProp = false; PropertyChanged("MyProp"); to make this work, but feels to clunky (and wastes some performance as well).
Tomáš Kafka
+1  A: 

A more general question to ask is: "Why am I trying to deal with this event in my ViewModel?"

If the answer has anything to do with view-only things like animations, I'd argue the ViewModel needs not know about it: code behind (when appropriate), Data/Event/PropertyTriggers, and the newer VisualStateManager constructs will serve you much better, and maintain the clean separation between View and ViewModel.

If something needs to "happen" as a result of the event, then what you really want to use is a Command pattern - either by using the CommandManger, handling the event in code behind and invoking the command on the view model, or by using attached behaviors in the System.Interactivity libs.

Either way, you want to keep your ViewModel as "pure" as you can - if you see anything View-specific in there, you're probably doing it wrong. :)

JerKimball
I don't think you understood the question well - let's say my Model is a twitter client that has event 'new message arrived'. ViewModel is listening on Model, and signals a 'NewMessageArrived' event as well (and it has no idea how does View react on this event). Now, I'd like the View to start an animation when the event occurs. There is no state change (see my comments above), so DataTrigger, PropertyTrigger and VisualStateManager are out of game, and EventTrigger listens on RoutedEvents, that originate only in UIElements (so the ViewModel cannot raise them!).
Tomáš Kafka
+2  A: 

Hi Tomas, thanks for pointing out your question.. Karma points much appreciated ;)

Use blend behaviors and existing triggers and actions to fulfill Tomas's requirement:

http://stackoverflow.com/questions/1424202/how-can-i-have-a-wpf-eventtrigger-on-a-view-trigger-when-the-underlying-viewmodel/1996324#1996324

Phil
Here you go, thanks :))
Tomáš Kafka