Ok, so I'm trying to grasp the concept of WPF Commands. They seem pretty straightforward, until you try to forward the command to something that's not the XAML's code behind. I've seen several ways to do this with custom commands, but no straightforward explanation of how to do this with the built in WPF commands. Things like "Open", "save", "cut", etc.
Is there a simple way, using the RelayCommand class or something else, to forward the Open command to my ViewModel?
WPF's built-in ApplicationCommands
and the like were not originally designed with MVVM in mind, so they don't really match up when you try to put them in the ViewModel.
In WPF 4 it is now possible to bind InputCommands
to your ViewModel:
https://www.thomaslevesque.com/2009/10/26/vs2010-binding-support-in-inputbindings/
An alternative is to use a DelegateCommands
, which you can implement on your own or get from a library like Prism. You can define the DelegateCommand
as an object in your viewmodel and bind to it from your view.
Once you have the binding working you define what the command does in your viewmodel code.
If you're doing MVVM with WPF, I would strongly recommend looking at a framework to help you out, such as:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With