How to override the auto-generated context menu on ribbon controls

Ribbon for WPF Forum

Posted 3 months ago by Ilia Korjoukov
Version: 21.1.3
Avatar

Hi,

I have a custom control in a ribbon tab, which has its own context menu. In newest Ribbon versions, that menu is ignored and the auto-generated QAT menu is shown on the right-mouse click. Is there a way to override the QAT menu for selected controls?

I made a test with one of Ribbon demos and found that a custom context menu is also ignored on the ribbon button. Here is the code I used:

<ribbon:Button>
  <ribbon:Button.ContextMenu>
    <ribbon:ContextMenu>
      <MenuItem Header="Test"/>
    </ribbon:ContextMenu>
  </ribbon:Button.ContextMenu>
</ribbon:Button>

Comments (10)

Answer - Posted 3 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Thanks for reporting this.  It appears to be due to a change made in v18.1.  We have made some updates for the upcoming v22.1 that should allow custom ContextMenus on controls in the Ribbon again.


Actipro Software Support

Posted 3 months ago by Ilia Korjoukov
Avatar

Thanks a lot in advance for fixing it! Do you know an aproximate release date for v22.1?

Posted 3 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You're welcome!  We are aiming for November or early December.


Actipro Software Support

The latest build of this product (v22.1.0) was released 1 month ago, which was before the next post in this thread.
Posted 1 month ago by Ilia Korjoukov
Avatar

Hi again!

I've just check this issue with the new version, 22.1, and it still there. My custom context menu does not override the QAT menu. Is there any workaround to suppress the QAT menu and show mine?

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ilia,

I just pasted your original ribbon:Button sample code in our Ribbon demo in multiple places and added a Label to it so it shows up.  When I right click the instances, I see the context menu with the "Test" menu item and don't see our generated context menu with QAT-related items. 

Are you positive you are on v22.1?  Please test in our v22.1 Ribbon demo and if you still see a problem, let us know detailed steps to reproduce it.  Thanks!


Actipro Software Support

Posted 1 month ago by Ilia Korjoukov
Avatar

Hi!

I double-checked it and I am surely using v22.1. It seems like the custom context menu works for <ribbon:Button> but does not for non-Ribbon custom controls.

Here I paste a code snippet that shows the issue:

 <ribbon:Ribbon IsToggleMinimizationButtonVisible="False">

        <ribbon:Tab Label="Test">
            
            <ribbon:Group>
                <ribbon:Button Label="Button">
                    <!-- this context Menu works -->
                    <ribbon:Button.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Action 1" />
                        </ContextMenu>
                    </ribbon:Button.ContextMenu>
                </ribbon:Button>

                <Grid
                    Width="80"
                    Height="80"
                    Background="Green">
                    
                    <!-- this context Menu is ignored-->
                    <Grid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Action 1" />
                        </ContextMenu>
                    </Grid.ContextMenu>
                </Grid>                
            </ribbon:Group>
            
        </ribbon:Tab>

    </ribbon:Ribbon>
Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ilya,

Thanks for the updated sample.  We had to alter some logic to allow for non-Ribbon-oriented controls to support custom context menus.  The update for that will be in the next maintenance release.  That may not be for another month or more, so if you'd like a preview build to try out sooner, please write our support address and mention this thread.


Actipro Software Support

Posted 1 month ago by Ilia Korjoukov
Avatar

Hi,

I would be definitly interested in a preview build, thank you!

Meanwhile, is there any simple workaround that I can use a temporal solution? For example, the Ribons docs mention that a context menu can be customized programmatically by intercepting the ContextMenuOpening. Would you recomend to try it?

Posted 1 month ago by Ilia Korjoukov
Avatar

ok, just figured out a simple solution for whom it might be interesting. First, I subscribed to OnContextMenuOpeningEvent as descibed here

There, I open the control's (Grid in this example) context menu programmically and cancel the QAT auto-generated menu with the following code:

 private void OnContextMenuOpeningEvent(object sender, ContextMenuEventArgs e)
            {           
            if (sender is Grid Gr)
                {                
                Gr.ContextMenu.PlacementTarget = Gr;
                Gr.ContextMenu.IsOpen = true;
                e.Handled = true; /// cancel the QAT menu
                }
            }
Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Yes that would work.  If you'd like the preview build to try our code change, please so this update isn't needed, please write our support email address and reference this thread.  We will send you the preview build link privately.


Actipro Software Support

Add Comment

Please log in to a validated account to post comments.