2010-08-10 46 views
1

在我的wpf窗口中,我有一个图像按钮。有没有人有任何想法如何为此按钮指定一个快捷方式,如“Cntrl + O”。图像按钮的wpf -shorcut键

我可以把“_”触发点击正常按钮。

<Button Margin="89,73,114,106" Name="button1" Click="button1_Click"> 
     <StackPanel Name="_StackPanel"> 
      <Image Source="image.png" ></Image> 
     </StackPanel>    
    </Button> 
+0

你应该看一看指挥和键绑定:HTTP:/ /msdn.microsoft.com/en-us/library/system.windows.input.keybinding.aspx – 2010-08-10 14:08:18

回答

1

在WPF常规键盘快捷键(不像Alt键的特殊情况)没有分配给按钮,它们被分配给操作。当你想要一个按钮(或菜单项,多个按钮等)和一个键盘命令用于同一个动作时,你可以使用一个命令。对于定制的RoutedCommand您可以分配KeyGestures火命令:

public static RoutedCommand MyCommand { get; private set; } 

    static Window1() 
    { 
     MyCommand = new RoutedCommand("MyCommand", typeof(Window1), new InputGestureCollection { new KeyGesture(Key.O, ModifierKeys.Control) }); 
    } 

    public Window1() 
    { 
     InitializeComponent(); 
     CommandBindings.Add(new CommandBinding(MyCommand, (_, e) => MessageBox.Show("Command fired"))); 
    } 

然后还要将其指定为Button的命令:

<Button Content="Click Me" Command="{x:Static local:Window1.MyCommand}"/>