1

I could be going about this the wrong way. I am trying to put as little code behind in the view as possible, so I want the CanExecute and Executed events of my command binding to be handled in my viewmodel, which is my data context.

I'm sure I'm missing something really simple, but I can't think how to do it.

The XAML for the command binding:

<UserControl.CommandBindings>
    <CommandBinding Command="DataControls:ParameterCollectionViewModel.UpdateCollection"
                    CanExecute="???"
                    Executed="???"
                    />
</UserControl.CommandBindings>

DataControls:ParameterCollectionViewModel.UpdateCollection is a static RoutedCommand, but the data context is of type ParameterCollectionViewModel (not that it matters).

How do I get the event handlers in my data context to be the event handlers in the command binding?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90

2 Answers2

2

You would probably be much happier if you used a DelegateCommand or something similar (an implementation also exists in the Microsoft Prism sources).

Jon
  • 428,835
  • 81
  • 738
  • 806
0

Simply said, you need to add the command to your datacontext. If the datacontext is a DependencyObject, you can simply add a new command to the Commands property.

If the datacontext is not a dependency property, I better hope it's a view model. In that case, you can use RelayCommand or DelegateCommand (actually, they are both the same).

I am one of the developers of Catel, an open-source MVVM framework, which also supports commands. You can find more information about Catel commands.

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