After studying several Q&A on stackoverflow, some tutorials and of course the official documentation, I trying to use ApplicationCommands
in my WPF Prism MVVM application.
My current approach
After trying different solutions I found out, I ended up with following constellation:
I am using the
AttachCommandBindingsBehavior
class mentioned in this answer, which will be used like this in the view:<UserControl> <i:Interaction.Behaviors> <localBehaviors:AttachCommandBindingsBehavior CommandBindings="{Binding CommandBindings}"/> </i:Interaction.Behaviors> </UserControl>
MyViewModel
contains aCommandBindingCollection
property, which will be populated in the constructor:public CommandBindingCollection CommandBindings { get; } = new CommandBindingCollection(); public MyViewModel() { this.CommandBindings.AddRange(new[] { new CommandBinding(ApplicationCommands.Save, this.Save, this.CanSave), new CommandBinding(ApplicationCommands.Open, this.Open) }); }
The UserControl
MyView
contains two buttons:<Button Command="ApplicationCommands.Open" Content="Open" /> <Button Command="ApplicationCommands.Save" Content="Save" />
My first question at this point is: Are the Executed()
and CanExecute()
methods already bound to the Command-DependencyProperty of the Button
? Since it does not work, what did I forgot or made wrong?
My second question is: How can I trigger the CanExecute
of the Command the Button is bound to? The actual use-case: MyViewModel.CanSave()
returns true, when the user successfully executed the MyViewModel.Open()
method. Usually, I would call an DelegateCommand.RaiseCanExecuteChanged()
, but calling ApplicationCommands.Save.RaiseCanExecuteChanged()
does not execute MyViewModel.CanSave()
.
Feel free to ask for more information. I will really appreciate your answers. Thank you!