CommandParameter is null if button activated by InputGesture

Ribbon for WPF Forum

Posted 15 years ago by Jason
Version: 4.5.0485
Platform: .NET 3.0
Environment: Windows XP (32-bit)
If Window2.Execute() is activated by pressing input gesture (F4), e.Parameter is null. If activated by mouse click, parameter is as expected ("param").
<Window x:Class="WpfApplication1.Window2"
    Title="Window2" Height="600" Width="600" xmlns:apr="clr-namespace:ActiproSoftware.Windows.Controls.Ribbon;assembly=ActiproSoftware.Ribbon.WPF30" xmlns:apc="clr-namespace:ActiproSoftware.Windows.Controls.Ribbon.Controls;assembly=ActiproSoftware.Ribbon.WPF30" xmlns:aps="clr-namespace:ActiproSoftware.Windows.DocumentManagement;assembly=ActiproSoftware.Shared.WPF30">
        <apr:Ribbon Name="ribbon" DockPanel.Dock="Top">
                <apc:ApplicationMenu KeyTipAccessText="">
                        <apc:RecentDocumentMenu Label="Recent Projects">
                            <aps:RecentDocumentManager x:Name="recentDocumentManager" />
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
using Button = ActiproSoftware.Windows.Controls.Ribbon.Controls.Button;
using ActiproSoftware.Windows.Controls.Ribbon.Input;

namespace WpfApplication1
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
        public Window2()
            Loaded += new RoutedEventHandler(Window2_Loaded);

        void Window2_Loaded(object sender, RoutedEventArgs e)
            ribbon.Tabs.Add(new Tab());
            ribbon.Tabs[0].Items.Add(new Group());

            RibbonCommand command = new RibbonCommand("button command", GetType());
            command.InputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.None));
            Button button = new Button();
            button.CommandParameter = "param";
            button.Label = "button";

            button.Command = command;

            CommandBinding binding = new CommandBinding(command, Execute);

        void Execute(object sender, ExecutedRoutedEventArgs e)
            string x = (string) e.Parameter; 
[Modified at 02/14/2009 06:26 AM]

Comments (2)

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

That is correct and is how WPF commands are designed to work by Microsoft. When you click on a control, the control instance passes the command parameter (from its CommandParameter property) to the command's Execute method.

When you press a hotkey instead, the CommandManager executes the command directly without looking at controls that use the command. There could potentially be many UI controls using that particular command, each with various command parameters. The command itself doesn't have a command parameter, thus nothing is passed to Execute.

So this would happen even if you had a normal ToolBar or Menu with the same setup.

Actipro Software Support

Posted 15 years ago by Phil Devaney - Senior Software Engineer, Serck Controls Ltd
Instead of adding a KeyGesture to the command, you could add a KeyBinding to your Window's InputBindings collection. A KeyBinding allows you to specify the command and parameter to execute when the specified KeyGesture is performed. KeyBindings are specified on a UIElement instead of a command, and only apply if that element has keyboard focus.
The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.