Command binding with Ribbon context menu

Ribbon for WPF Forum

Posted 15 years ago by Anurodh Ora
Version: 4.5.0486
Avatar
Hi,

We are creating a context menu dynamically where we are trying to set its ".Command" property to execute custom operation on click of particular menu item of context menu.
But when context menu is populated, the menu items sets with ".Command" property are displays as "disabled".
Following is the code sample:

RibbonControls.ContextMenu mContextMenu = new RibbonControls.ContextMenu();
RibbonControls.Button objBtn = new RibbonControls.Button();

objBtn.Name = "btnCustom";
objBtn.Label = "CustomOperation";
objBtn.ScreenTipHeader = "Custom operation";
objBtn.Command = CustomCommands.cmdCustOperation;
mContextMenu.Items.Add(objBtn);

//Code for executing the command
 public class CustomCommands
{
        public static RoutedCommand cmdCustOperation = new RoutedCommand();

App.mMainWindow.CommandBindings.Add(new CommandBinding(CustomCommands.cmdCustOperation , cmdCustOperation_Executed,cmdCustOperation_CanExecute));

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

        private void cmdCustOperation_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //Custom operation
        }
}
Now when we run this code, it shows the context menu but with disabled "CustomOperation" menu item. If we comment the line where we are setting the ".Command" property then that menu item is enabled.

We are not understanding where we are making mistake?

Please share the sample code, it will be helpful.

Thanks,
Aurodh

[Modified at 04/12/2009 01:59 AM]

Comments (2)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Aurodh,

This probably has nothing to do with our controls. Try making a regular WPF ContextMenu with a normal WPF MenuItem on it that has the command and see if the same thing happens. It most likely will. Let us know if that is not the case.

Command routing can be tricky sometimes, perhaps you'll have to set the PlacementTarget of the ContextMenu before you show it. You can search on the web for lots of information on help with command routing.


Actipro Software Support

Posted 15 years ago by Anurodh Ora
Avatar
Thanks for your reply.

Yes for normal WPF MenuItem also it was not working. It works after setting the CommandTarget property to correct object/target.

Thanks again for your help.
The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.