Add RibbonCommand from code-behind

Jun 9, 2009 at 1:38 AM


I want to add RibbonApplicationMenuItem and the corresponding RibbonCommand from code-behind but I can't figure out where to specify the CanExecute and Executed.
This is how I specify the menu item and command and then link the command to the menu item.

        Dim ribmnuitmTemplate As New Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenuItem
        Dim ribcmdTemplate As New Microsoft.Windows.Controls.Ribbon.RibbonCommand
        ribcmdTemplate.LabelTitle = "This is the Command title"
        ribcmdTemplate.LabelDescription = "This is the Command description"
        ribmnuitmTemplate.Name = "This is the Item Name"
        ribmnuitmTemplate.Command = ribcmdTemplate
        ribmnuitmTemplate.CommandParameter = "Parameter"

In XAML I  can specify the CanExecute and Executed on the RibbonCommand and it works fine, but these properties are not available from code-behind.

Any help is greatly appreciated!


Jun 11, 2009 at 10:52 AM

Hello again,

Nobody has done this?

Getting a bit desperate as my deadline is approaching so any suggestions are very welcome!


Jun 11, 2009 at 12:46 PM

Hi Aklahr,

We have been struggling with using the RibbonControl as well and as you probably already know, it is being totally rewritten with a whole new API and may be released late 2009 (from what I've heard :) As far as the current Ribbon goes, have you looked at the walkthrough on the WindowsClient site. I think the example that they show there demos what you are trying to do.



Jun 11, 2009 at 12:51 PM

Hi Bill,

Thanks for your reply. I have seen the walkthrough on the WindowsClient site. They are adding the RibbonCommands from the XAML which I am already doing without problem.

My problem is how to do it from code-behind as my commands will be dynamic.




Jun 11, 2009 at 5:37 PM

I think what you need to do is add a new CommandBinding to your window's CommandBindings collection.  It takes as arguements the ICommand (RibbonCommand) and the CanExecute and Executed Handlers.