Customizing WPF Commands from Multiple View Models

Like any MVVM WPF application, I have several view models. Each of them has several teams. My view uses Fluent UI (Office Ribbon), so there are some elements that light up depending on the context of the application. The feed is a child of the main application.

The main structure of my application is that it manages the COURSE. There are several MODULES in COURSE, so I have a virtual machine for the course and module ... and each has commands.

When the application loads, I set the data context of the main window to the course, so linking the course commands to the tape is easy and works fine.

The task arises when the user starts working with the module. When a module is selected from the list, parts are displayed in another user control. Now ... my task is to connect the commands to the tape.

I assume that I may have some kind of event handler that programmatically passes the current module commands to all the corresponding controls on the tape and deletes everything when the context leaves. But this seems like a lot of unnecessary work. Is there a cleaner way to do this?

I was thinking about routed commands / events, but someone told me that this would not work, because they would not bubble all the way to the window and return to the tape.

Looking for some kind of guidance here ... I am a bit like MVVM (but I love it!).

+3
1

: ShellCommands, .

public class ShellCommands : IShellCommands
{
    public ICommand SaveCommand { get; set; }
    ...
}

CourseViewModel ModuleViewModel .

public class CourseViewModel : ViewModel 
{
    public CourseViewModel(IShellCommands shellCommands, ...)
    {
        this.ShellCommands = shellCommands;
        ...
    }

    public IShellCommands ShellCommands { get; private set; }
}

XAML ShellCommands.

<MenuItem Header="Save" Command="{Binding ShellCommands.SaveCommand}"/>

.

: WPF Application Framework (WAF)

+4

Source: https://habr.com/ru/post/1744381/


All Articles