Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi
Can System.Windows.Interactivity and Microsoft.Expression.Interactions (ExpressionBlend SDK) be used to setup and handle both dependency properties and routed events denoted by the wrench symbol and the lighting symbol in Visual Studio 2012?

I have a obtained a sample of a window that contains a textbox when the textbox is changed (dependency property) a method MyMethod in class MyViewModel : INotifyPropertyChanged is invoked. This is close to what I want.

Also in the window is a MediaElement where the event MediaOpened="mediaPlayer1_MediaEnded_1" (routed event) is handled in the code behind:
class MainWindow : Window

I’m trying to comply with MVVM style.

How should the xaml and c# code be modified such that the routed event MediaOpened is handled in class MyViewModel?

See http://database.hugetiger.com/dbExplore/MVVMCode.aspx
Posted

1 solution

Your problem is that you can't bind to a View-level event from a view-model. That breaks the no-visual-dependency rule for the view model. So you have to covert the routed event into a command.

The most common way to work around this is to use the EventToCommand class in the MVVMLight toolkit. It is the easiest way to do it. However, there is a similar function in the Blend libraries if you don't want to use MVVMLight. It is in the Interactivity namespace. This link should help: http://blogs.msdn.com/b/luc/archive/2010/11/18/executing-a-command-from-an-event-of-your-choice.aspx[^]

Either way you will get the same result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900