Wpf Ribbon Control

BigH61 581 Reputation points
2022-08-26T12:55:42.213+00:00

I would like to be able to invoke a command when a RibbonTab is selected following MVVM. I am generally aware of how to instigate commands but I am unable to find a way to Bind the command to a Ribbon Control Tab.
Any assistance would be appreciated and an example of the RibbonTab xaml binding would be great.

Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. BigH61 581 Reputation points
    2022-09-05T10:52:41.957+00:00

    I finally came up with two solutions for my problem both involving Behaviors.

    Solution One involved the addition of a "behaviors:EventTrigger" as below:

    xaml

    <behaviors:Interaction.Triggers>  
                    <behaviors:EventTrigger EventName="SelectionChanged">  
                        <behaviors:InvokeCommandAction Command="{Binding RibbonTabSelectionChangedCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=RibbonMainWindow}"/>  
                    </behaviors:EventTrigger>  
    </behaviors:Interaction.Triggers>  
      
    

    Then within the View Model the Command as below:

    ViewModel.cs

     public RelayCommand RibbonTabSelectionChangedCommand { get; private set; }  
          
        public NavigationViewModel()  
        {  
               RibbonTabSelectionChangedCommand = new RelayCommand(RibbonTabSelectionChanged);  
        }  
      
    public void RibbonTabSelectionChanged(object parameter)  
    {  
          Ribbon ribbon = (Ribbon)parameter;  
         if (ribbon.SelectedIndex == 3)  
         {  
              SelectedViewModel = new ImageViewModel();  
         }  
         else  
         {  
              SelectedViewModel = new CollectionViewModel();  
         }  
    }  
      
    

    Solution Two involved the addition of the below to the above xaml solution

    xaml

    <behaviors:Interaction.Behaviors>  
                    <bh:MainWindowRibbonBehavior/>  
    </behaviors:Interaction.Behaviors>  
      
    

    Along with the addition of the “MainWindowRibbonBehavior” class

    internal class MainWindowRibbonBehavior : Behavior<Ribbon>  
        {  
            protected override void OnAttached() =>  
           AssociatedObject.Loaded += AssociatedObject_Loaded;  
            private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) =>  
              ((NavigationViewModel)(AssociatedObject.DataContext)).RibbonMainWindow = AssociatedObject;  
        }  
      
    

    Finally the View Model was amended as follows:

    public Ribbon RibbonMainWindow { get; set; }  
      
    public NavigationViewModel()  
    {  
           RibbonTabSelectionChangedCommand = new RelayCommand(RibbonTabSelectionChanged);  
    }  
      
    public void RibbonTabSelectionChanged(object parameter)  
    {  
           if (RibbonMainWindow.SelectedIndex == 3)  
           {  
               SelectedViewModel = new ImageViewModel();  
           }  
           else  
           {  
               SelectedViewModel = new CollectionViewModel();  
           }  
    }  
      
    

    I trust someone may find this useful?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.