Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 MVVM
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-Feb-13 4:45am
Greg811163

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 253
1 Maciej Los 205
2 Nguyen.H.H.Dang 190
3 Peter Leow 185
4 arvind mepani 162


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid