Add Command to Ribbon Quick Access Toolbar

Nov 19, 2008 at 5:54 PM
Hi,
Is there an easy way to allow a user to right click on a ribbon button, and select "Add to Quick Access Toolbar"?

Thanks,
aSchimp
Coordinator
Nov 19, 2008 at 7:43 PM
Hi aSchimp,

You would just need to implement a ContextMenu with an item for "Add to Quick Access Toolbar" and when the user selects that item, you would want to create a copy of the control that the user had clicked on (for example, if it was a RibbonButton, just create a new button and assign the same command to it) and add the control to the Quick Access Toolbar with the Placement attached property set to InToolbar.

If you need help implementing this, let me know.

Thanks!
Samantha
Coordinator
Dec 4, 2008 at 12:57 AM
In case anyone would like an example, I wrote a quick sample showing how to do this:

In the XAML:

 <r:RibbonWindow x:Class="RibbonAddToQAT.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="Window1" Height="500" Width="500"
Background="White">
     <r:RibbonWindow.Resources>
         
<r:RibbonCommand x:Key="myCommand"
              
LabelTitle="Button1"
              
SmallImageSource="icon.png"
              
LargeImageSource="icon.png"
              
CanExecute="RibbonCommand_CanExecute"
              
Executed="RibbonCommand_Executed"/>
    
</r:RibbonWindow.Resources>
    
<Grid>
         
<r:Ribbon Name="myRibbon">
              
<r:RibbonTab>
                   
<r:RibbonGroup>
                        
<r:RibbonButton Command="{StaticResource myCommand}">
                             
<r:RibbonButton.ContextMenu>
                                  
<ContextMenu>
                                       
<MenuItem Header="Add to Quick Access Toolbar" Click="AddToQAT"/>
                                  
</ContextMenu>
                             
</r:RibbonButton.ContextMenu>
                        
</r:RibbonButton>
                   
</r:RibbonGroup>
              
</r:RibbonTab>
         
</r:Ribbon>
    
</Grid>
</
r:RibbonWindow>

In the code behind:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Windows.Controls.Ribbon;

namespace RibbonAddToQAT
{
    
public partial class Window1 : RibbonWindow
    
{
         
public Window1()
          {
               InitializeComponent();
          }

    
private void AddToQAT(object sender, RoutedEventArgs e)
     {
         
// Step 1 - Find out what type of control is being added to the QAT
         
Type controlType = ((ContextMenu)((MenuItem)sender).Parent).PlacementTarget.GetType();

         
// Do a check for each control type in your Ribbon to determine what type the sending control is
         
// This is an example of how to do it for RibbonButton
         
if (controlType == typeof(RibbonButton))
          {
              
// Step 2 - Clone the control
              
// 2A - Once you know the type, create a new instance of that type
              
RibbonButton newButton = new RibbonButton();

              
// 2B - Assign the same RibbonCommand to the new instance of the control as is used on the original instance.
              
// This will make sure that the controls have the same visual and command properties
              
newButton.Command = ((RibbonButton)((ContextMenu)((MenuItem)sender).Parent).PlacementTarget).Command;

              
// Step 3 - Add the new control to the QuickAccessToolBar
              
myRibbon.QuickAccessToolBar.Items.Add(newButton);
               }

// Other Considerations
// - Control states. You may need to make sure that the control states are in sync. For example, if you have toggle buttons, the toggle state
// of the control on the QAT should be the same as the corresponding control on the Ribbon.
// - Serialization. You may want to serialize the controls in the QAT so that the same ones appear the next time the user opens the application
// - Scalability. In this case, we've defined the ContextMenu on the control itself, but in a real-world scenario, this approach doesn't really scale.
// Instead, what you'd want to do is to set the ContextMenu at the Ribbon level. However, this will break the check for the control type, since
// PlacementTarget will always return Ribbon. To find the control that was clicked, you can handle the PreviewMouseUp event and use it in the
// context menu handler.
         

         
private void RibbonCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
          {
               e.CanExecute =
true;
          }

         
private void RibbonCommand_Executed(object sender, ExecutedRoutedEventArgs e)
          {
              
//Not implemented
         
}
     }
}

 

 

 

Feb 4, 2009 at 8:12 PM
Are there any examples or guidance on how to serialize the QAT?