2012-04-10 149 views
0

下面是我試圖爲菜單項分配快捷方式。當我點擊菜單項時效果很好,但快捷方式不起作用。任何想法?菜單項快捷方式

<Window.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Save" Executed="MyCommand" /> 
</Window.CommandBindings> 

<Window.InputBindings> 
    <KeyBinding Key="S" Modifiers="Control" Command="ApplicationCommands.Save"/> 
</Window.InputBindings> 

<MenuItem Header="Save" Name="MainMenu_File_Save" Command="ApplicationCommands.Save"> 
    <MenuItem.Icon> 
     <Image Height="16" Width="16" Source="/NewGUI_WPF;component/Images/saveHS.png" /> 
    </MenuItem.Icon> 
</MenuItem> 

private void MyCommand(object sender, ExecutedRoutedEventArgs e) {...} 
+0

偉大的問題。它已經在這裏回答http://stackoverflow.com/questions/4682915/defining-menuitem-shortcuts – 2012-04-10 18:16:01

+0

我知道這個鏈接。但我仍然無法找到我的錯誤。 – Shibli 2012-04-10 18:28:45

+0

你可以將整個xaml和代碼放在後面嗎?至於我一切正常。也許其他內部使用相同的命令? – 2012-04-10 18:52:17

回答

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
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.Navigation; 
using System.Windows.Shapes; 

namespace test 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void MyCommand(object sender, ExecutedRoutedEventArgs e) 
     { 
      MessageBox.Show("Test"); 
     } 
    } 
} 

<Window x:Class="test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Save" Executed="MyCommand" /> 
    </Window.CommandBindings> 

    <Window.InputBindings> 
    <KeyBinding Key="S" Modifiers="Control" Command="ApplicationCommands.Save"/> 
    </Window.InputBindings> 

    <Grid> 
    <Menu IsMainMenu="True"> 
    <MenuItem Header="_File"> 
     <MenuItem Header="Save" Name="MainMenu_File_Save" Command="ApplicationCommands.Save" /> 
    </MenuItem> 
    </Menu> 
    </Grid> 
</Window> 

這對我的作品。對你起作用嗎 ?

+0

當我點擊父母菜單,然後快捷方式它的作品,但沒有其他。 – Shibli 2012-04-10 19:51:01

+0

我修改過的示例,它仍然有效。應用程序的窗口是否被激活? – 2012-04-10 20:16:47

+0

問題是否只存在於菜單中的xaml中? – 2012-04-10 20:24:38