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) {...}  
4

1 回答 1

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>

这对我行得通。对你起作用吗 ?

于 2012-04-10T19:13:09.297 回答