C# 输入绑定和WebBrowser控件
我有一个非常简单的应用程序,试图将键盘快捷键绑定到绑定到菜单项的WPF命令。应用程序本身只包含一个C# 输入绑定和WebBrowser控件,c#,wpf,menu,commandbinding,inputbinding,C#,Wpf,Menu,Commandbinding,Inputbinding,我有一个非常简单的应用程序,试图将键盘快捷键绑定到绑定到菜单项的WPF命令。应用程序本身只包含一个菜单和一个WebBrowser控件 当我在WebBrowser中时,键盘快捷键不会发送到WPF菜单。例如,在web浏览器中聚焦时键入“Ctrl+O”将显示IE打开的页面。此外,在这个应用程序中,除非我有菜单焦点(通过键入Alt),否则输入绑定不会启动。例如,我不能通过单击标题栏然后键入快捷方式来关注WPF窗口。完整代码复制如下: main window.xaml <Window x:Class
菜单
和一个WebBrowser
控件
当我在WebBrowser
中时,键盘快捷键不会发送到WPF菜单。例如,在web浏览器中聚焦时键入“Ctrl+O”将显示IE打开的页面。此外,在这个应用程序中,除非我有菜单焦点(通过键入Alt),否则输入绑定不会启动。例如,我不能通过单击标题栏然后键入快捷方式来关注WPF窗口。完整代码复制如下:
main window.xaml
<Window x:Class="TestInputBindingsOnMenu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Menu IsMainMenu="True" x:Name="_mainMenu" Grid.Row="0" />
<WebBrowser Source="http://google.com" Grid.Row="1" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace TestInputBindingsOnMenu
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
MenuItem fileMenu = new MenuItem();
MenuItem fileNew = new MenuItem();
MenuItem fileOpen = new MenuItem();
MenuItem fileExit = new MenuItem();
fileMenu.Header = "File";
fileNew.Header = "New";
fileOpen.Header = "Open";
fileExit.Header = "Exit";
fileMenu.Items.Add(fileNew);
fileMenu.Items.Add(fileOpen);
fileMenu.Items.Add(fileExit);
_mainMenu.Items.Add(fileMenu);
var fileNewCommand = CreateCommand("New");
var fileOpenCommand = CreateCommand("Open");
var fileExitCommand = CreateCommand("Exit");
_mainMenu.CommandBindings.Add(new CommandBinding(fileNewCommand, ExecuteNew));
_mainMenu.CommandBindings.Add(new CommandBinding(fileOpenCommand, ExecuteOpen));
_mainMenu.CommandBindings.Add(new CommandBinding(fileExitCommand, ExecuteExit));
fileNew.Command = fileNewCommand;
fileOpen.Command = fileOpenCommand;
fileExit.Command = fileExitCommand;
_mainMenu.InputBindings.Add(new InputBinding(fileNewCommand, new KeyGesture(Key.N, ModifierKeys.Control)));
_mainMenu.InputBindings.Add(new InputBinding(fileOpenCommand, new KeyGesture(Key.O, ModifierKeys.Control)));
_mainMenu.InputBindings.Add(new InputBinding(fileExitCommand, new KeyGesture(Key.F4, ModifierKeys.Alt)));
}
private void ExecuteNew(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("New!!");
}
private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Open!!");
}
private void ExecuteExit(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Exit!!");
}
private static RoutedCommand CreateCommand(string label)
{
return new RoutedCommand(label, typeof(MainWindow));
}
}
}
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
命名空间TestInputBindingsOnMenu
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
初始化();
}
私有void初始化()
{
MenuItem fileMenu=新建MenuItem();
MenuItem fileNew=new MenuItem();
MenuItem fileOpen=new MenuItem();
MenuItem fileExit=new MenuItem();
fileMenu.Header=“File”;
fileNew.Header=“新建”;
fileOpen.Header=“打开”;
fileExit.Header=“Exit”;
fileMenu.Items.Add(fileNew);
fileMenu.Items.Add(fileOpen);
fileMenu.Items.Add(fileExit);
_mainMenu.Items.Add(文件菜单);
var fileNewCommand=CreateCommand(“新建”);
var fileOpenCommand=CreateCommand(“打开”);
var fileExitCommand=CreateCommand(“退出”);
_添加(newcommandbinding(filenewcommandandexecutenew));
_添加(新的CommandBinding(fileOpenCommand,ExecuteOpen));
_添加(新的CommandBinding(fileExitCommand,ExecuteExit));
fileNew.Command=fileNewCommand;
fileOpen.Command=fileOpenCommand;
fileExit.Command=fileExitCommand;
_添加(新的输入绑定(fileNewCommand,newkeypership(Key.N,ModifierKeys.Control));
_添加(新的InputBinding(fileOpenCommand,新的Key手势(Key.O,ModifierKeys.Control));
_添加(新的输入绑定(fileExitCommand,newkeypostation,Key.F4,ModifierKeys.Alt));
}
私有void ExecuteNew(对象发送方,ExecutedRoutedEventArgs e)
{
MessageBox.Show(“新建!!”);
}
私有void ExecuteOpen(对象发送方,ExecutedRoutedEventArgs e)
{
MessageBox.Show(“打开!!”);
}
私有void ExecuteExit(对象发送方,ExecutedRoutedEventArgs e)
{
MessageBox.Show(“退出!!”;
}
专用静态路由命令CreateCommand(字符串标签)
{
返回新的RoutedCommand(标签、类型(主窗口));
}
}
}
简易解决方案
将输入绑定添加到WebBrowser控件以及主菜单:
Browser.InputBindings.Add(new KeyBinding(ApplicationCommands.Open,
new KeyGesture(Key.O, ModifierKeys.Control)));
硬解决方案
这里发生的情况是,ui元素
正在使用KeyDown
事件,而您希望使用PreviewKeyDown
事件,或者添加一个也处理Handled
路由事件的处理程序。如果你不知道这件事,请查查
因为这是在UIElement类中处理的,所以我建议在这种情况下使用不同的模式。提供一个EventToCommand
行为。如果您可以将窗口的PreviewKeyDown
事件路由到右侧命令,而不是使用KeyBinding
和ui元素的InputBindings
集合,您将获得解决方案
您需要一些自定义代码来检查按下了哪个键以及路由应该是哪个命令。这基本上满足了我的要求,除非焦点不在浏览器或菜单上。我尝试将命令绑定添加到主窗口,但也没有帮助。在上面的示例中,例如(将绑定添加到浏览器控件后),如果我键入其中一个命令(弹出一个消息框),然后单击“回车”,在再次单击浏览器控件之前,我将无法再执行任何快捷方式。请尝试将它们添加到浏览器和主窗口中!我确实将它们添加到了浏览器和主窗口以及菜单中。