.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > WPF events with parameters using MVVM (Microsoft surface)
 

WPF events with parameters using MVVM (Microsoft surface)

I'm working on a Microsoft surface app in WPF using the MVVM design pattern.

I want to catch and handle an event for when a tag is placed on the surface. This is like a mousedown event except that information in contained about the contact in the event args (the type and value if it is a tag for instance). Because of the arg I can't do my (actually Josh Smith's) relaycommand workaround.

Any suggestions on handling events with parameters with MVVM?

Thanks in advance

Here is a sample of the code if it where to call event handlers in the code behind (Not really of any value ;)
<Rectangle Fill="Transparent"
           s:Contacts.ContactDown="OnContactDown"
           s:Contacts.ContactChanged="OnContactChanged"
           s:Contacts.LostContactCapture="OnLostContactCapture">
        </Rectangle>
Dan dot Net
Hello,

In MVVM you need to use Attached Behaviors to deal with events. An attached behavior allows (among other things) to execute a command in response to an event.
Of course, when you handle that event, you can pass all its arguments as parameter to a command.

if you need information about Attached behaviors see this:

http://joshsmithonwpf.wordpress.com/2008/08/30/introduction-to-attached-behaviors/

Good luck
  • Marked As Answer byDan dot Net Tuesday, September 08, 2009 5:11 PM
  •  
HomeroThompson
Can you pass your event arguments as a CommandParameter?
dmikon
What would that look like in code. I was hoping to link the event to a command in xaml like:

<Rectangle Fill="Transparent"
         s:Contacts.ContactDown="{Binding Path=OnContactDown}">
 </Rectangle>
But that obviously doesn't handle the arg. Would I need to handle the event in code behind and then call the command in MVVM somehow (setting the arg as the CommandParameter there)???
Dan dot Net
Hello,

In MVVM you need to use Attached Behaviors to deal with events. An attached behavior allows (among other things) to execute a command in response to an event.
Of course, when you handle that event, you can pass all its arguments as parameter to a command.

if you need information about Attached behaviors see this:

http://joshsmithonwpf.wordpress.com/2008/08/30/introduction-to-attached-behaviors/

Good luck
  • Marked As Answer byDan dot Net Tuesday, September 08, 2009 5:11 PM
  •  
HomeroThompson
I looked at Josh's article and tried to apply it to my situation. In the attached behavior class his new behavior is triggered by registering for the 'selected' event of the treeviewitem:

item.Selected += OnTreeViewItemSelected;

How would you accomplish this if the event was not a 'native' event to the object? For instance, I my surface app I want my new attached behavior to be associated with the event 'OnContactDown' (which is under the surface namspace and not directly associated with the rectangle class). I.e.:

s:Contacts.ContactDown="OnContactDown" (s being the surface namespace)

Thus I can't just type:

item.ContactDown
Dan dot Net
Well, I figured out how to link the behavior to a non native event

RoutedEventHandler handler = OnTreeViewItemSelected;
item.AddHandler(Microsoft.Surface.Presentation.Contacts.ContactDownEvent, handler);

Now I just need to figure out how to call a command from the behavior


Dan dot Net

You can use google to search for other answers

Custom Search

More Threads

• Skin implentation based on User Preferences
• DataTemplate is not applied when customize a ListView
• Get all bindings on a page?
• Gridview row selection on key up
• Window Events
• Expanding one level in a treeview
• How to set up binding so that one property updates another property with same value.
• Animation in SplashScreen is possible?
• WindowState.Maximized hides the TaskBar
• MediaElement fails to play WMV file giving an “Exception from HRESULT: 0xC00D10BD?error