Silverlight 4: Drag&drop with EventToCommand

One of the MVVM Light Toolkit’s user requested that I add the possibility to pass the EventArgs of an event to the ICommand that it is bound to through the EventToCommand trigger. At first I was a bit reluctant because it seems like a transgression of the rule that says you should avoid to have too much knowledge about the UI layer in the ViewModel. For example, if you have a RelayCommand in the ViewModel that expects a MouseEventArgs, it kinds of binds you to a certain kind of UI element, which is not super clean.

Read the rest of this entry »

Print | posted on Thursday, December 17, 2009 4:49 AM


# re: Silverlight 4: Drag&drop with EventToCommand

left by Laurent at 12/19/2009 6:57 AM Gravatar
Hey Fallon,

About your first question: The Messenger is designed to be completely independant from any constraint, so there is, in my opinion, no reason that it shouldn't work on the server. Make sure that you use the correct version (the one that says "WPF" and not "Silverlight", because the server side needs the full .NET.

Second question: I am a big fan of MEF, and I would like to encourage you to integrate MEF and MVVM Light Toolkit, this should work just fine. I recently created a prototype in Silverlight 4 for a client and used MEF and MVVM Light, with great success. I try to keep the toolkit as light as possible, and am a bit careful not to grow it too much. I do however believe in making it as compatible as possible with other frameworks such as MEF, and I think that right now it is in good shape in that respect.

That said, if you have specific ideas, feel free to suggest where you think that attributes might help, and I will check that out ;)


# re: Silverlight 4: Drag&drop with EventToCommand

left by Azadeh at 3/4/2010 6:16 PM Gravatar

Thank you for this great framework.

But I just faced an issue so it would be appreciated if you can give me a solution:
I want to send a parameter to one of ViewModel via its Constructor (ViewModel’s Constructor) but this is the locator who is responsible to create or clear the ViewModels so I can’t do that. Of course this issue can be solved by using Messenger(somehow) , But I’m looking a more practical and professional way to overcome this issue.

Any solutions could be useful.


# re: Silverlight 4: Drag&drop with EventToCommand

left by Michael at 3/19/2010 12:13 PM Gravatar
Thank you for this!

# re: Silverlight 4: Drag&drop with EventToCommand

left by nrupal at 3/25/2010 4:12 AM Gravatar
Thank you for the MVVM Light toolkit. It was a good presentation at MIX10.

A few quick questions:
1. Is it possible to send the drop point information to the viewmodel?
2. Is it possible to add children to a Canvas from viewmodel ( by data binding to the children property - this approach doesn't work as children property doesn't allow for databinding)

A work around for the above two problems is by developing a custom canvas control and custom DragEventArgs (which has the position information in it with respect to the custom canvas).


# re: Silverlight 4: Drag&drop with EventToCommand

left by Shadowdog at 7/15/2010 8:46 AM Gravatar
Laurent - you are the Bomb!!! Keep up the good work and keep adding these great examples!!!

THANK-You. :)

# re: Silverlight 4: Drag&drop with EventToCommand

left by mark baer at 8/30/2010 9:19 AM Gravatar
Excellent article. I was able to get it working...I am trying to do something similar, but wish to drag a ListBoxItem onto a Scrollviewer where I then dynamically add fields/etc...I cant figure out how to handle the dragged item. It is a File object in your code, but I'm not sure what to do with a listitem. I'm sure it's something simple. Can anyone help me out with this?


# re: Silverlight 4: Drag&drop with EventToCommand

left by Eric Smith at 9/15/2010 4:54 AM Gravatar
Thanks for the framework. I'm just beginning with Silverlight, but I'm a proponent of DDD and MVC, so this is a natural fit for my SL entry.

By the way, I was able to step into my HandleDropCommand Implementation in VS 2010.

# re: PassEventArgsToCommand

left by Andrey Tsvetkoff at 11/29/2010 10:46 AM Gravatar
Hello Laurent! Thank you for your framework.

I've got a question about the theoretical side of the above-referenced property ('PassEventArgsToCommand').

We can use it to be more declarative, and it's good. But we simultaneously violate the principle of dependency inversion — our ViewModel depends on the UI-framework specific XXXEventArgs class.

From the other side, we could use codebehind class to handle the DropEvent, and convert it to a Command call, with the FileInfo[] passed as parameter. It's more DI-friendly, but also more coddy.

My idea is - maybe the golden mean - that we can create the Converter in code (which converts from the UI-specific XXXEventArgs to some business-specific object) and pass that converter declaratively to the 'EventToCommand', via smth like 'EventArgsToCommandArgsConverter' property.

This way, we minimize the coding efforts, and also avoid the VM to depend on UI Framework. Our command has a parameter of 'FileInfo[]', which it is actually interested in.

What do you think of it?

# re: Silverlight 4: Drag&drop with EventToCommand

left by Gonzalo Rossello at 4/21/2011 5:59 PM Gravatar
But how do you start a drag using mvvm pattern?

# re: Silverlight 4: Drag&drop with EventToCommand

left by Laurent Bugnion at 4/25/2011 10:31 PM Gravatar

In my opinion, a drag operation is a matter of the view, strictly. The result of it (a "drop" for instance) may be communicated to the VM for treatment but the drag itself should be handled in the view.

Comments have been closed on this topic.