2

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

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

2 回答 2

1

看起来您需要在方法中添加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>

后面的代码:

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");
        }
    }
}

请注意,如果您注释掉该行:

e.CanExecute = true;

键绑定不再起作用。

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

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="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");
        }
    }
}
于 2012-04-10T20:44:52.667 回答
1

Focusable="False"在您的Window标签中删除。

于 2012-04-10T21:56:36.003 回答