2

I have a ViewModel which contains a Command which is exposed via the CommandManager to other Views.

Now I want to use EventToCommand to fire the Command if I double click on a item of a TreeView (https://catelproject.atlassian.net/wiki/display/CTL/EventToCommand).

Unfortunate the Command isn't triggered if I use catel:CommandManagerBinding, a normal Binding to a Command of the ViewModel of the View is working fine. But I need to bind to a Command of another ViewModel.

thanks in advance

  • Are you able to use the CommandManagerBinding without the EventToCommand? In other words, are you sure your command gets registered correctly? – Geert van Horrik Jun 21 '17 at 07:57
  • Yes, I'm using the Command for a MenuItem which is doing exactly the same. And it's working fine on the MenuItem. – ClemensGerstung Jun 21 '17 at 08:00
  • Can you please create a small repro so this can be fixed in Catel v5? It should work since the CommandManagerBinding is just a global thing. – Geert van Horrik Jun 21 '17 at 08:03
  • I've created a testproject with this bug https://github.com/HumpaLumpa007/EventToCommand.CommandManagerBinding . I will also create an issue on the github project page – ClemensGerstung Jun 21 '17 at 09:14

2 Answers2

0

What about using InvokeCommandAction?

0

I've responded to the ticket created on the issue tracker, but will put the reply here usability as well.

Note this has been tested with Catel v5, but should work with v4 as well.

I made 2 changes:

1.I create the command at startup:

var commandManager = ServiceLocator.Default.ResolveType<ICommandManager>();
commandManager.CreateCommand("AddToCollectionCommand");

Then I register an action for the command inside the VM:

commandManager.RegisterCommand("AddToCollectionCommand", AddToCollectionCommand, this);

2.You have to bind the command first, then the command parameter in xaml (order matters, although it shouldn't, but that's a xaml parsing issue we can't fix):

<!-- now works! -->
<catel:EventToCommand Command="{catel:CommandManagerBinding AddToCollectionCommand}" 
                                         CommandParameter="{Binding SelectedDataItem}"
                                         DisableAssociatedObjectOnCannotExecute="False" />

I recommend that you also look into application-wide command containers. They are extremely powerful and allow you to separate concerns. You can find the docs here:

http://docs.catelproject.com/vnext/catel-mvvm/commands-events/application-wide-commands

If you use Orchestra (see https://github.com/wildgums/orchestra), you can even remap the app-wide commands input gestures with a ready-to-use UI.

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32