当我单击菜单条然后按快捷键时,它可以工作,但不起作用。是否有设置快捷键范围的东西?
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) {...}
答案 0 :(得分: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");
}
}
}
答案 1 :(得分:1)
删除Focusable="False"
代码中的Window
。