I have already read Josh Smiths article about binding commands to view model using RelayCommand. However I need to bind ApplicationCommands.Save to a view model so that when a user clicks the save menu item it is handled in the window. How is it possible?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
There is a good tutorial to help bind application commands.
Setup attached properties. Read the article on how to do that.
Then bind to it using attached properties.
How is your save menu item being created? Somewhere it must be getting bound to the ApplicationCommands.Save? You can change that so that it binds to your ViewModel's save instead.
The best solution I'm aware of us to use a service. For example, an ICommandBindingsProvider like this:
This gets injected into your ViewModel and used like this:
How the service gets injected will be dependent on what MVVM framework you're using. Most (but not all) support some sort of service injection capabilities. If you need a more concrete code example, look at Onyx which does service injection and has a service that does just this.
Or, you place the save command binding on the viewmodel, and then databind to it, something like (pseudo code)
in the viewmodel:
in the view:
Update: i just tried binding to a command in a commandbinding and it doesn't work. what i was thinking of was binding to a command.
I have used this approach successfully in the past:
Start out by defining a CommandBindings property on ViewModels (same definition as on Views).
Then for each Command implemented by ViewModel, add a CommandBinding to ViewModel's CommandBindings.
Then, when you set:
Also set:
I think may be done using a binding of the CommandBindings property (can't remember for sure).