wpfprismribboncontrolslibrary

Prism and Ribbon: Crossing regions with commands


I am building a composite WPF application with Prism and using the Ribbon Library. One thing I am having a very difficult time with is with region-crossing commands.

For example, I have my ribbon in my 'RibbonRegion' and a grid view in my 'MainRegion'. Lets say I want a button in my ribbon to pop up a message box with the currently selected item in the grid view, how do I do this?

The easy way is using EventAggregator but I fear that if I have a bunch of subscribers hooked up just for button clicks I am just asking for memory leak issues.

Is there a way to have a cross-region command, so that clicking a button in my 'RibbonRegion' will get the selected item in the grid view and pop up a message box with that value?


Solution

  • You can use a System.Windows.Input.RoutedUICommand

    First you need to declare your Command as below:

    public static class Commands
    {
        public static readonly RoutedUICommand TestCommand = new RoutedUICommand("Test Command",
            "Test Command", typeof(Commands));
    }
    

    Then in your RibbonRegion xaml:

    <my:RibbonButton Command="{x:Static cmd:Commands.TestCommand}" ...
    

    Then in your MainRegion xaml:

    <UserControl.CommandBindings>
        <CommandBinding CanExecute="OnTestCanExecute"
                        Command="{x:Static cmd:Commands.TestCommand}"
                        Executed="OnTestExecute" />
    </UserControl.CommandBindings>
    

    Then in your xaml.cs:

    public void OnTestRouteCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    
        public void OnTestRouteExecute(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            // do some stuff here
        }