views:

1011

answers:

3

Just wondering what people had for ideas on how best to handle events in a ViewModel from controls on a View ... in the most lightweight way possible.

Example:

<MediaElement
     MediaOpened={Binding SomeEventHandler} />

In this case we want to handle the MediaOpened event in a ViewModel. Without a framework like Prism, how would one bind this to a ViewModel?

+3  A: 

Commanding - your 'SomeEventHandler' needs to be a class that implements ICommand... there's a heap of literature available online...

Also - I would consider getting a free, lightweight 'mini' MVVM framework, such as MvvmFoundation, which provides the RelayCommand for just such a purpose (without the complexity/overhead of learning PRISM)

EDIT:

Have a look at this blog for attaching command to any event... It is incredibly powerful, as I mentioned, but I guess you do need to make a judgement call if this is what you want, compared with something like attaching an old-fashioned event, and using a super-slim event handler in your code behind that simply invokes some method on your ViewModel, something like:

public void SomeEventHandler(object sender, SomeEventArgs e)
{
    MyViewModel vm = (MyViewModel)this.DataContext;
    vm.HandleLoadEvent( );
}

pragmatic vs Best-practise... I'll leave it with you ;)

IanR
I didn't think I could use an ICommand in this sort of scenario ... thanks for the info ... this is a bit of a repeat question then.
Chris Nicol
Ok ... I just tried using the ICommand, I'm using MVVMFoundation, so it's a RelayCommand type. However, I get a build error "Binding Path=MediaOpenedCommand, Mode=OneTime}' is not a valid event handler method name ..." Am I just setting it up wrong?
Chris Nicol
sorry, yes... you will need to bind the RelayCommand instance to a Command, and tie this back to the UI via CommandBindings or InputBindings... I strongly recommend you do a wee bit of reading - switching from events to commands is one of those "steep learning curves" that people keep on about with WPF - but so powerful when you grok it!!
IanR
I get Command binding when you have a "Command" property on controls like ... Button, Hyperlink, etc. I can't find any examples or explanations on creating a Command property on a control that can handle something like the example above. I get that I need to bind the RelayCommand to a "Command" ... but how do I create a command on something like the MediaElement that can execute my ICommand when the Source has loaded (equivalent to when the MediaOpened event would fire).
Chris Nicol
updated answer...
IanR
A: 

MediaOpened is an event and does not support command binding.

In order to bind to the event a helper object may be used which attaches to the event and executes a command.

To bind to the view model add a property which implements ICommand. Figure 3 in this MSDN magazine article shows the RelayCommand which is a useful implementation of ICommand. RelayCommand is initialized with a delegate to connect to your view model.

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Doug Ferguson
I get the Command Binding issue and I've read Josh Smith's article on MVVM, but the part I'm having a problem with is the "to bind to the event a helper object may be used which attaches to the event and executes a command".By helper object do you mean like an Attached Behavior?
Chris Nicol
Yes, a Behavior is a good way to make the binding. The link that IanR provided looks good.
Doug Ferguson
+3  A: 

Have a look at Marlon Grech's Attached Command Behaviors. It makes it easy to bind events to ViewModel commands

Thomas Levesque