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 に答える