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 5: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 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100