C# 菜单快捷键的范围

C# 菜单快捷键的范围,c#,wpf,xaml,menuitem,keyboard-shortcuts,C#,Wpf,Xaml,Menuitem,Keyboard Shortcuts,当我点击菜单条,然后按快捷键,它可以工作,但它不工作,否则。有什么东西可以设置快捷键的范围吗 XAML: <Window x:Class="NewGUI_WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:common="clr-nam

当我点击菜单条,然后按快捷键,它可以工作,但它不工作,否则。有什么东西可以设置快捷键的范围吗

XAML:

<Window x:Class="NewGUI_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="clr-namespace:Common;assembly=RecentFileListLib"
    Title="Sezor" Height="Auto" Width="Auto" WindowStartupLocation="Manual" Icon="/NewGUI_WPF;component/Images/i161.ICO" WindowStyle="SingleBorderWindow" Focusable="False" ResizeMode="CanResize" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="243" d:DesignWidth="314" SizeToContent="Manual" WindowState="Maximized">

<Window.CommandBindings>
    <CommandBinding Command="Save" Executed="SaveCommand" />
    <CommandBinding Command="Open" Executed="OpenCommand" />
</Window.CommandBindings>

<Grid>
    <Menu Height="23" Name="main_menu" VerticalAlignment="Top" HorizontalAlignment="Stretch" IsMainMenu="True">
        <MenuItem Name="MI_Case" Header="Case">
            <MenuItem Header="Open" Command="Open">
                <MenuItem.Icon>
                    <Image Height="16" Width="16" Source="/NewGUI_WPF;component/Images/openHS.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Save" Name="MainMenu_File_Save" Command="Save">
                <MenuItem.Icon>
                    <Image Height="16" Width="16" Source="/NewGUI_WPF;component/Images/saveHS.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Save as" Name="MainMenu_File_SaveAs" Click="MainMenu_File_SaveAs_Click" />
            <common:RecentFileList x:Name="RecentFileList" />
            <MenuItem Header="Quit" Click="MainMenu_File_Quit_Click" />
        </MenuItem>
        <MenuItem Header="View">
            <MenuItem Header="Input File" Click="MainMenu_View_InputFile_Click" />
            <MenuItem Header="Mesh File" Click="MainMenu_View_meshFile_Click" />
            <MenuItem Name="MainMenu_View_Summary" Header="Summary" Click="MainMenu_View_summary_Click" />
        </MenuItem>
        <MenuItem Header="Define" >
            <MenuItem Header="Input File" Click="MainMenu_Define_InputFile_Click" Name="MainMenu_Define_InputFile" />
            <MenuItem Header="Mesh File" Click="MainMenu_Define_MeshFile_Click" Name="MainMenu_Define_MeshFile" />
            <MenuItem Header="Simulation File" Click="MainMenu_Define_SimulFile_Click" Name="MainMenu_Define_SimulFile" />
            <MenuItem Header="Boundaries" Click="MainMenu_Define_BC_Click" />
            <MenuItem Header="Initials" Click="MainMenu_Define_Initials_Click" />
            <MenuItem Header="Spatial Discretization" Click="MainMenu_Define_SpatDis_Click" />                
            <MenuItem Header="Flow" Click="MainMenu_Define_Flow_Click" />
            <MenuItem Header="Material" Click="MainMenu_Define_Material" />
            <MenuItem Header="Algoritm" Click="MainMenu_Define_Algoritm" />
            <MenuItem Header="Gradient Reconstruction">
                <RadioButton Content="Least-Squares" Checked="Least_Squares_Checked" Name="rad_GR_LS" />
                <RadioButton Content="Green-Gauss" Click="Green_Gauss_Checked" Name="rad_GR_GG" />
            </MenuItem>
        </MenuItem>
        <MenuItem Header="Run" >
            <MenuItem Header="Simulation" Click="MainMenu_Run_Simulation_Click" />
        </MenuItem>
    </Menu>
    <Frame Height="Auto" HorizontalAlignment="Left" Margin="40,60,30,20" Name="frm_summary" VerticalAlignment="Top" Width="Auto" NavigationUIVisibility="Hidden" />
    <StatusBar Height="23" Name="statusBar1" VerticalAlignment="Bottom" Margin="0">
        <TextBlock Name="statBar_text_1"></TextBlock>
    </StatusBar>
</Grid>
private void OpenCommand(object sender, ExecutedRoutedEventArgs e) {...}  
private void SaveCommand(object sender, ExecutedRoutedEventArgs e) {...}  

看起来您需要将
CanExecute
添加到
CommandBinding
和相应的代码隐藏方法中

下面是一个简单的演示,它连接
Control-s
以启动
Help
命令

XAML:

<Window x:Class="stackoverflow___scope_of_menu_shortcut_key.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="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted"/>
    </Window.CommandBindings>

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

    <Menu>
        <MenuItem Header="File">
            <MenuItem Header="Help" Name="HelpMenu" Command="Help"/>
        </MenuItem>
    </Menu>

</Window>
密钥绑定不再起作用

这是你的例子,精简到最低限度。这适用于我的系统:

XAML:


代码隐藏:

using System.Windows;
using System.Windows.Input;

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

        private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.microsoft.com");
        }
    }
}
using System.Windows;
using System.Windows.Input;

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

        private void MyCommand(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.microsoft.com");
        }
    }
}
使用System.Windows;
使用System.Windows.Input;
命名空间堆栈溢出\uuuuuuuu范围\u菜单\u快捷键\u
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有void MyCommand(对象发送方,ExecutedRoutedEventArgs e)
{
系统.诊断.进程.启动(“http://www.microsoft.com");
}
}
}

在您的
窗口中删除
Focusable=“False”

我按照您的建议进行了更改,但情况仍然相同。嗯。。。我在上面逐字逐句地将您的代码输入到一个新的WPF项目中,它实际上对我来说运行良好。我正在运行Windows 7,并使用Visual Studio 2010进行编译。你用的是什么?另外,你能试试我贴在下面的例子,看看这在你的系统上是否也不起作用吗?您可能想在另一个开发框上测试这一点,以防在您使用的开发框上出现奇怪的输入问题。此外,由于
control-s
Save
命令的默认值,因此您似乎可以忽略
窗口。InputBindings
部分。我在我的系统上这样做了,密钥绑定工作正常。我的也是win7和VS10。问题是,我的菜单在网格中,而你的不是。我试过你的,效果不错。这可能是因为网格的缘故吗?您能否发布一个演示该问题的最小示例的完整XAML和代码?即网格内的菜单?:-)我将下面的最小示例包装在一个网格中,并且密钥绑定仍然在我的系统上运行。@shibli没问题!很高兴我们发现了问题。发布准确的代码总是有帮助的。;-)
<Window x:Class="stackoverflow___scope_of_menu_shortcut_key.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="Save" Executed="MyCommand" />
    </Window.CommandBindings>

    <MenuItem Header="Save" Command="ApplicationCommands.Save"/>

</Window>
using System.Windows;
using System.Windows.Input;

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

        private void MyCommand(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.microsoft.com");
        }
    }
}