Locked RibbonButton with .NET 4.0 and VS2010 command

  • Friday, March 30, 2012 4:18 PM
     
      Has Code

    I am trying to create a ribbon to use with my WPF application. Right now I am just trying to learn how to create the ribbons and put in the functionality. When I try to add a command to my RibbonButton, I can't get it to work. I know that RibbonCommand is not available in the 4.0 format.

    Here is my XAML code:

    <r:RibbonWindow x:Class="FASTBeamUI.MainWindow"
            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="MainWindow" Height="350" Width="525">
        <Grid>
            <r:Ribbon Title="FB Ribbon - Document1" x:Name="ribbon">
                <r:Ribbon.CommandBindings>
                    <CommandBinding 
                        Command="ClipboardCommand"
                        CanExecute="CanExecute"
                        Executed="Execute"
                     />
                </r:Ribbon.CommandBindings>
                    <r:RibbonTab Header="Home">
                    <r:RibbonGroup Name="Clipboard" >
                        <r:RibbonButton Background="#FF9B0000" BorderBrush="#FFD2DA00" 
                                        LargeImageSource="/FASTBeamUI;component/Images/gplogo.png" 
                                        Label="Clipboard" Command="ClipboardCommand" 
                                        CommandParameter="This is test text"></r:RibbonButton>
                    </r:RibbonGroup>
                    <r:RibbonGroup Name="Fonts">
                        <r:RibbonButton Content="Fonts"></r:RibbonButton>
                    </r:RibbonGroup>
                </r:RibbonTab>
                <r:RibbonTab Header="Insert"></r:RibbonTab>
                <r:RibbonTab Header="Help"></r:RibbonTab>
            </r:Ribbon> 
        </Grid>
    </r:RibbonWindow>

    And here is the ClipboardCommand class:

        public class ClipboardCommand:ICommand
        {
            public void Execute(object parameter)
            {
                string msg = parameter as string;
                MessageBox.Show(msg, "Test command");
            }
            public bool CanExecute(object parameter)
            {
                return true;
            }
            public event EventHandler CanExecuteChanged;
        }

    The error message I get is "Cannot convert ClipboardCommand"

    I don't know what to do, but I'm sure one of you smart people has used RibbonButtons in .NET 4.0 and knows how to help me.

    Thanks

All Replies

  • Sunday, April 08, 2012 10:32 AM
     
      Has Code

    I would suggest the following:

    1. I looked at this http://www.codeproject.com/Articles/22270/WPF-Commands and would suggest to create static RoutedCommand instead of implementing ICommand (never got it working). :

    namespace FASTBeamUI
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : System.Windows.Controls.Ribbon.RibbonWindow
        {
            public static RoutedCommand ClipboardCommandR = new RoutedCommand();
    
            public void ClipboardCommandRExecuted(object parameter, ExecutedRoutedEventArgs e)
            {
                string msg = parameter as string;
                MessageBox.Show(msg, "Test command");
            }
            public void ClipboardCommandRCanExecute(object parameter, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }
    
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    }

    Note that I'm using .NET Framework 4.5 but I did exactly the same earlier with Microsoft.Windows.Controls.Ribbon or so.

    2. Be sure to add local namespace to XAML:

    <r:RibbonWindow x:Class="FASTBeamUI.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:r="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon"
            xmlns:local="clr-namespace:FASTBeamUI"
            Title="MainWindow" Height="350" Width="525">

    3. Rewrite your command binding appropriately to the new code-behind structure:

                <r:Ribbon.CommandBindings>
                    <CommandBinding 
                        Command="local:MainWindow.ClipboardCommandR"
                        CanExecute="ClipboardCommandRCanExecute"
                        Executed="ClipboardCommandRExecuted"
                     />
                </r:Ribbon.CommandBindings>

    And it should work fine. More reading here: http://www.codeproject.com/Articles/238657/How-to-use-Commands-in-WPF. I hope this helps (well it compiles and runs here ;))

    ---

    Chris