菜单项快捷方式

本文关键字:快捷方式 菜单项 | 更新日期: 2023-09-27 18:30:43

以下是我尝试为菜单项分配快捷方式的内容。当我单击菜单项时,它工作正常,但快捷方式不起作用。知道吗?

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

菜单项快捷方式

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>

这对我有用。它对你有用吗?