Like any MVVM WPF app I have a handful of view models. Each has a few commands. My view implements a Fluent UI (Office ribbon) so there are some items that light up based on the context of the application. The ribbon is a child to the main application.
The basic structure of my app is that it manages a COURSE. A COURSE has multiple MODULES in it, so I have a VM for course & module... and each has commands.
When the app loads I set the data context of the main window to the course so binding the course commands to the ribbon is easy and works fine.
The challenge comes when the user starts to work with a module. When a module is selected from a list the details are shown in another user control. Now... my challenge is how to wire up the commands to the ribbon.
I assume I could have some event handler that programatically wires up the current module's commands to all the relevant controls in the ribbon and removes everything when the context goes away. But that seems like a lot of unnecessary work. Is there a cleaner way of doing this?
I thought about routed commands/events, but someone told me that this wouldn't work because they won't bubble all thew ay up to the Window and back down to the ribbon.
Looking for some guidance here... I'm a bit of a noob to MVVM (but loving it!).
Idea: Introduce a ShellCommands class which is exposed as a service.
public class ShellCommands : IShellCommands
{
public ICommand SaveCommand { get; set; }
...
}
Then the CourseViewModel and the ModuleViewModel can use the same service to register their commands.
public class CourseViewModel : ViewModel
{
public CourseViewModel(IShellCommands shellCommands, ...)
{
this.ShellCommands = shellCommands;
...
}
public IShellCommands ShellCommands { get; private set; }
}
In XAML you can access the service via the ShellCommands property.
<MenuItem Header="Save" Command="{Binding ShellCommands.SaveCommand}"/>
.
More Informations: WPF Application Framework (WAF)
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