MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

Update (11/14/2009): I updated EventToCommand with a new feature: You can now pass the EventArgs of the fired event to the invoked command. See this post for more explanations.

Here is another early release of the MVVM Light Toolkit V3 (Alpha 2). I decided to release gradually and early as soon as a new part of this new version is ready, to allow advanced users to install, test and give feedback about the new features. If you haven’t seen it yet, the features available in V3 Alpha 1 are described in a previous article. There will be a few more alphas before I make a V3 release, which means more good things are a-coming ;)

Read the rest of this entry »

Print | posted on Thursday, November 5, 2009 2:57 AM

Feedback

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent Kempé at 11/5/2009 3:19 AM Gravatar
You did it again! Congrats Laurent for this GREAT step :)

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by thesiteman at 11/5/2009 4:05 AM Gravatar
we are not worthy...we are not worthy...we are not worthy...

Thanks and Great job...this is going to be awesome!

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by bhrnjica at 11/6/2009 12:56 AM Gravatar
Great work,
EventToCommand works great in VS2010 beta2 as well.

# Great Job!

left by Dan Wahlin at 11/6/2009 5:25 AM Gravatar
Great stuff Laurent! I haven't been a fan of commanding in Silverlight at all since it was so limited (thinking of Prism) without writing some custom code. Your solution lets me handle all the events I have to deal with nicely...well done!

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Kuba at 11/9/2009 9:36 AM Gravatar
On Codeplex there is apparently a file missing (It's in the solution but not in repository):

After checkout from svn:
\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras\Command\EventToCommand.WPF.cs' could not be opened

# I love it!

left by Kuba at 11/9/2009 9:57 AM Gravatar
Just got the binaries from the link in the article. Your Toolkit is a joy to use. Simple yet powerful, and everything just works, EventToCommand is great!

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Martin Hinshelwood at 11/11/2009 12:16 AM Gravatar
Looks fantastic :)

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by P.M. Goupil at 11/23/2009 10:27 PM Gravatar
J'ai un problème avec "EventToCommand Command" dans un DataTemplate. Un même bouton qui fonctionne avec la commande 'SimpleCommand' dans une page ne fonctionne plus s'il est déplacé dans un DataTemplate.
(MVVM Light Toolkit V3 Alpha 2).
Cordialement

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent at 11/24/2009 10:23 PM Gravatar
Hello,

Je viens d'essayer ici et ca fonctionne, avec le template dans les resources locales, et aussi dans un ResourceDictionary. Peut-etre un problem de DataContext? Envoie moi ton projet (laurent (at) galasoft (dot) ch) et je regarde volontiers.

Amities,
Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Olivier at 1/8/2010 11:28 PM Gravatar
Merci Laurent pour ce super boulot.
MVVM Light est de loin ma lib préférée, ton approche pragmatique est la bonne.
Just hâte de voir une doc qui se tient :-) Mais c'est toujours le plus dur à boucler !
(mais ça serait vraiment mieux pour communiquer sur ton toolkit, là c'est un peu éparpillé et c'est vraiment trop fait pour les geeks alors que le toolkit est simple à utiliser et qu'il mérite d'être diffusé et utilisé largement. Je peux te donner un coup de main si tu veux... )

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent at 1/9/2010 12:55 AM Gravatar
Merci pour les mots sympas, ca fait plaisir. Oui, la doc, je sais. Comme je suis en train d'ecrire mon nouveau livre sur Silverlight 4, pas trop de temps libre... cela dit, si tu veux ecrire des articles, je suis ravi de les publier sur le site du toolkit!
http://www.galasoft.ch/mvvm/getstarted/#blogs

Amities,
Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Nasir at 1/28/2010 8:00 AM Gravatar
This is a great toolkit and am having fun with it. Except one issue, I tried EventToCommand behavior in Silverlight 4, seems like its not firing the command. Have you seen or tested this? Do we have to do anything different in SL4? Thanks for all your work!

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent at 1/28/2010 8:58 AM Gravatar
Hi,

Yes it was tested in SL4 and should work fine. Make sure that you are using the SL4 DLLs and not the SL3. Using the SL3 DLLs, it will build but will not execute correctly.

When I release the final V3 version, I will change the names of the DLLs to make it more obvious which one if for which version of the framework.

Let me know if this solves the issue,
Thanks,
Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Ilyasse at 4/1/2010 2:48 AM Gravatar
Vos librairies sont vraiment très utiles. Mais j'ai un problème à l'exécution lorsque je veux binder une simple commande Click dans ma View à mon ViewModel.

J'ai le message suivant

La propriété pouvant être attachée 'Triggers' est introuvable dans le type 'Interaction

<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GM:EventToCommand Command="{Binding SimpleCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

Vous savez pourquoi ? J'utilise les dll pour la version 4 de Silverlight. Merci d'avance...

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by pM at 4/2/2010 2:15 AM Gravatar
Bonjour...Je kiff grave aussi ce toolKit. 2 choses:
- Pour le post précédent (Ilyasse): je confirme le meme problème. Je dois faire référence à .Interactivy d'une version inférieure
- Rien ne passe sur un SelectionChanged pour le code suivant:
<ComboBox Width="200" ItemsSource="{Binding Path=Themes}" Height="25" SelectedValuePath="Theme" SelectedValue="{Binding Path=CurrentNote.Theme, Mode=TwoWay}" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" BorderThickness="1" BorderBrush="{Binding Path=ForeColor}">
<StackPanel Background="{Binding Path=BackColor}" Height="20" Margin="0">
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="200" Text="{Binding Path=Name}" Foreground="{Binding Path=ForeColor}"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding SelectChangeCommand}"></cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
Merci d'avance

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Jean Marc at 4/2/2010 5:52 AM Gravatar
Bonjour Laurent,

Jusque quelques mots pour dire un grand merci.
J'ai passé beaucoup de temps à essayer de comprendre le 'pattern' MVVM jusqu'à présent sans succès.

Finalement et pour être bref, MVVM Light toolkit: Framework + Snippets + Exemples = en 1 mot, SO EASY !

Bonne continuation!

PS:superbe présentation au MIX10!

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Miguel Angel Santiago at 4/11/2010 10:09 PM Gravatar
Dear Laurent,

Thank you very much for this great functionality EventToCommand.

I was trying to apply it on the events from the MS Surface, but I am having troubles to catch the actual events from Surface Controls (I guess they inherit from FrameworkElement). For instance:

Let the traditional method be:

<s:SurfaceButton s:Contacts.ContactDown="OnContactDown"
</s:SurfaceButton>

now I change to:

<s:SurfaceButton>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContactDown">
<cmd:EventToCommand Command="{Binding ContactCommand, Mode=Default}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceButton>

However, it does not raise the Command. I've checked that my code is right by simply replacing the event ContactDown by the traditional Click.

Could you tell how could I make work the events of Surface into Commands of the MVVM?

Thank you very much in advance.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Miguel Angel Santiago at 4/12/2010 12:42 AM Gravatar
Sorry, there was a mistake in my code. It is not s:SurfaceButton what I really meant, but another object which not includes ContactDown as native event. For instance: a rectangle which aims to catch the ContactDown.

Let me make it clearer:

<Rectangle s:Contacts.ContactDown="OnContactDown"
</Rectangle>

ant the objective would be:

<Rectangle>
<i:Interaction.Triggers>
<i:EventTrigger EventName="s:Contacts.ContactDown">
<cmd:EventToCommand Command="{Binding ContactCommand, Mode=Default}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceButton>

In the meanwhile, I've found out an interesting solutiong from: http://bit.ly/aWcEOS

Therein, it suggests to code a RouteEventTrigger to solve the recovery of the EventName from Reflection. I've updated the code like this and it works:

<Rectangle>
<i:Interaction.Triggers>
<mvvmjoy:RoutedEventTrigger RoutedEvent="s:Contacts.ContactDown">
<cmd:EventToCommand Command="{Binding ContactCommand, Mode=Default}"/>
</mvvmjoy:RoutedEventTrigger>
</i:Interaction.Triggers>
</s:SurfaceButton>

In any case, if you suggesst any other solution, it is also welcome.

Thank you very much.

# EventToCommand does not exist in namespace

left by Dan at 4/27/2010 8:23 AM Gravatar
I get the following error when trying to use in Windows7, VS10.

The tag 'EventToCommand' does not exist in XML namespace 'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras'.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Mike Lockyer at 5/17/2010 7:03 AM Gravatar
I am trying to use the EventToCommand to provide a MVVM solution to the SElectItem item issue as identified by
https://www.west-wind.com/Weblog/posts/567249.aspx

But with no luck

Can I set CommandParameter to the selected item in a list box

Thanks

Mike

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent at 5/20/2010 9:01 PM Gravatar
Hi Mike,

For SelectedItem (singular), use a data binding to a property on the target object (typically this is a ViewModel).

For SelectedItems (plural), see http://blog.galasoft.ch/archive/2010/05/19/handling-datagrid.selecteditems-in-an-mvvm-friendly-manner.aspx

Cheers,
Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Gary Howlett at 7/15/2010 12:05 AM Gravatar
How can I use EventToCommand on a TreeView when I wish to actually attach to TreeViewItem.Expanded?

Thanks

Gary

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Blobi at 8/15/2010 9:36 PM Gravatar
Bonjour,

Est-il possible de lier un Behavior ajouté à une gestion d'événement EventToCommand ?

Je pose cette question car j'ai ajouté la gestion du
double click sur mon datagrid par un Behavior et j'aimerai gérer cette événement avec les commandes.

Merci pour votre aide

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Nabeel at 8/20/2010 4:59 AM Gravatar
Hi Laurent,

Is it possible to pass more than one parameter to the command and is it possible to pass properties of EventArgs instead of passing the whole EventArgs ?

Regards,
Nabeel

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Someone at 9/19/2010 9:41 PM Gravatar
I have a weird problem with RelayCommand and MustToggleIsEnabled on Windows Phone 7: when the bound event occurs (its a button click) the canExecute Func<bool> is called before the actual execute Action. Because of this the IsEnabled state of my button reflects the state of the underlying instance before the action is beeing executed.
On the other hand, i am sure it was working before. Dont know what went wrong.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Someone at 9/19/2010 9:46 PM Gravatar
Maybe this behaviour is correct. But when MustToggleIsEnabledValue is true shouldnt the canExecute Func<bool> get called again after the execute Action was called? At least, my button remains enabled and i don't know whats wrong :-)

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Anonymous at 11/18/2010 10:45 PM Gravatar
Is it possible to bind custom events to teh command using "EventToCommand" ? I am currently evaluating the feasibility of using MVVMLight of our product and this is one think I am stuck up with. Also do you think MVVMlight will work better for CustomControls as well.. Any help is much appreciated..Thanka !

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Alberto at 11/23/2010 12:23 AM Gravatar
Hi,

I am new with MVVM and I was playing around with this framework. I create a small application and everything is working correctly when I only want to send one parameter to the command. But do you know how can I send more than one parameter to the command?

For example with a form with 3 or 4 text-boxes??

Thanks.
Alberto

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by aappddeevv at 12/4/2010 7:36 PM Gravatar
Similar to the comment above about TreeViewItems, I need to attach the EventToCommand to TreeViewItems being generated by a TreeView in order to capture the mouse double click event and turn it into a "Open in editor" command. How to attach this in XAML to the ItemContainerGenerator's output? Or will this need to be done in code-behind?

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Randy at 12/9/2010 9:25 PM Gravatar
Hi,

Thank you for the excellent work! We are using MVVM Light in our applications and it has helped tremendously.

My question is related to EventToCommand behaviors: SInce it is possible to create multiple EventToCommand elements for a single EventTrigger element in XAML, is there a way to specify the order in which the EventToCommand elements are processed (i.e. order in which the commands are executed)? My guess is that they will be executedi n the order in which they appear in the XAML, but I wasnt' sure.

Thanks again,
Randy

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Jean-Pierre Fouche at 1/18/2011 12:23 PM Gravatar
Hi Laurent,
Thank you for your great work.
I am trying to use the WPF extras, but it won't load the System.Windows.Interactivity.dll.

Any ideas why this may be the case? I have tried using the downloaded dll as well as the one available from the GAC.

Visual Studio 2010 (.NET 4, WPF).

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Alex at 1/19/2011 5:09 AM Gravatar
Hi Laurent,

I am trying to use the EventToCommand but I am getting the following error:

"The tag 'EventToCommand' does not exist in XML namespace 'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4'"

I've added the reference to "C:\Program Files\Laurent Bugnion (GalaSoft)\Mvvm Light Toolkit\Binaries\Silverlight4\System.Windows.Interactivity.dll" but still the error is there.

Any ideas on why this may be occurring ?

Thanks for your help
Regards
Alex

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent at 1/19/2011 8:11 AM Gravatar
Did you reference GalaSoft.MvvmLight.Extras.SL4.dll?

Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Alex at 1/20/2011 6:12 AM Gravatar
Yes, I did. Strangely, in design time IntelliSense sense picks up EventToCommand when I type cmd:
However, I get "The tag 'EventToCommand' does not exist.." error when compile my app.

Thank you,
Alex

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Madhur at 2/13/2011 5:23 PM Gravatar
I want to pass XamDataTree's SelectedItems from one view model to another. Do you think this is a good idea? and if yes how to do that? I see examples of simple parameters like TextBox.Text passed as parameters. Any help will be appreciated.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Alex at 3/7/2011 5:58 AM Gravatar
Hi Laurent,

I've realised lately that System.Windows.Interactivity.dll from MVVM Light Toolkit clashes somehow with the one from Microsoft.
I added the reference to "C:\Program Files\Laurent Bugnion (GalaSoft)\Mvvm Light Toolkit\Binaries\Silverlight4\System.Windows.Interactivity.dll" assembly using Add Reference/Browse option in VS 2010.
When I check its properties it shows reference to "C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries\System.Windows.Interactivity.dll"

I couldn't figure out what else I can do to force reference to the "C:\Program Files\Laurent Bugnion (GalaSoft)\Mvvm Light Toolkit\Binaries\Silverlight4\System.Windows.Interactivity.dll"?

Thanks for your help
Regards
Alex

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Daniel Freire at 3/9/2011 6:21 PM Gravatar
I discovered the problem:

Problem:

The tag 'EventToCommand' does not exist in XML namespace 'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras'.

Solution:

What is happening is that your dll is named differently, when you download the binary dll site is for WPF, Silverlight and Windows phone, and each has a different nomenclature for example to WPF is GalaSoft.MvvmLight.Extras.WPF4.dll and xaml pointing to this assembly=GalaSoft.MvvmLight.Extras assembly that does not exist then in the case of WPF should look like this:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"


I'm from Brazil
Original message in Portuguese translated into English in google.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Laurent Bugnion at 3/9/2011 11:56 PM Gravatar
@Alex: This is the same DLL (or maybe a slightly different, but compatible version). I provide it as part of MVVM Light for convenience only, so that the user does not have to install the Blend SDK to use EventToCommand.

Studio tends to replace the reference with the one to the Blend SDK if it is installed. This shouldn't cause an issue.

Cheers,
Laurent

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Sven De Baets at 3/10/2011 2:15 PM Gravatar
Daniel,

Had the same problem when upgrading from SL3 to SL4.

You're probably referencing the wrong version of System.Windows.Interactivity. The assembly version should be 4.0.5.0. Just take the one from the MVVM Light Toolkit package on CodePlex.

Gr,

Sven

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Sven De Baets at 3/10/2011 2:18 PM Gravatar
Typo ... my last message was ment for Alex, not Daniel.

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Andy at 7/31/2012 6:43 PM Gravatar
Hey Laurent,
Still using your toolkit for all WPF/SL/WP7 and now Win8 development... it's so great.

I have a quick question about the all-too-useful EventToCommand Behavior.

I'm building a WPF4 app that will be used interchangeably between a win8 touch device and desktop pc. So, instead of binding a button's Command property directly, I'm using EventToCommand to bind the Click and TouchUp events to separate commands on my view model (for different touch vs. desktop functionality).

I am using a CanExecute method on one of the commands to say if the selected item in a list is null, return false (disabled), else true (enabled) and have checked the "MustToggleIsEnabled" property of the EventToCommand Behavior.

When I load my view the button is disabled, as there is no currently selected item. However, when I select an item in the list it remains disabled. CanExecute isn't raised again, unlike when using the button's default Command property (it changes to enabled when using that).

Is this a limitation of EventToCommand? Do I need to specifically say in my view model "When selection changes, go check CanExecute?"

Thanks,
Andy

# re: MVVM Light Toolkit V3 Alpha 2: EventToCommand behavior

left by Florian at 8/7/2012 11:01 PM Gravatar
Hello,
I built some messaging for the same reason two years ago in my application and now I replace it with your framework! perfect! you spent much more details and I think also time to implement the messaging! Thank you!

But I have one issue:
I also used your EventToCommand (SL5 version) and the code works fine if i Bind to Events like Click, MouseEnter, MouseLeave, MouseMove. But binding to MouseLeftButtonDown and Up does not work (on a button or some events also on an image).

Are there any known issues?

Thx, Florian

Comments have been closed on this topic.