C# WPF WebBrowser以ALT+;按键已按下

C# WPF WebBrowser以ALT+;按键已按下,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我对WPF有一个非常奇怪的问题 一个简单的窗口,带有带有快捷方式(“确定”)和webbrowser控件的按钮。 问题是:当光标位于webbrowser内的文本区域时,当我按“o”时,wpf会在我按Alt+o时触发按钮单击 以下是Windows1.XAML的XAML: <Window x:Class="testBrowser.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x

我对WPF有一个非常奇怪的问题

一个简单的窗口,带有带有快捷方式(“确定”)和webbrowser控件的按钮。 问题是:当光标位于webbrowser内的文本区域时,当我按“o”时,wpf会在我按Alt+o时触发按钮单击

以下是Windows1.XAML的XAML:

<Window x:Class="testBrowser.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <WebBrowser Name="m_Browser" Focusable="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
                Margin="0"/>

    <TextBox Name="m_Text" Grid.Row="1" />
    <Button Name="m_Ok" Content="_Ok" Click="m_Ok_Click" Grid.Row="2" />
  </Grid>
</Window>

以及隐藏的代码(仅设置浏览器内容):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间测试浏览器
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
m_Browser.NavigateToString(“”);
}
私有无效m_确定_单击(对象发送方,路由目标)
{
MessageBox.Show(“按下Ok”);
}
}
}
编辑:使用此代码修复:

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 testBrowser
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();

      EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, 
                                        new AccessKeyPressedEventHandler(OnAccessKeyPressed));

      m_Browser.NavigateToString("<textarea cols=\"20\" rows=\"10\"/>");
    }

    private void m_Ok_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Ok pressed");
    }

    private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e)
    {    
      if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt) {
        e.Target = null;
        e.Handled = true;
      }
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间测试浏览器
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
EventManager.RegisterClassHandler(typeof(UIElement)、AccessKeyManager.AccessKeyPresseEvent、,
新的AccessKeyPressedEventHandler(OnAccessKeyPressed));
m_Browser.NavigateToString(“”);
}
私有无效m_确定_单击(对象发送方,路由目标)
{
MessageBox.Show(“按下Ok”);
}
按下AccessKeyPressed时出现私有静态无效(对象发送器、AccessKeyPressedEventArgs e)
{    
if((Keyboard.Modifiers&ModifierKeys.Alt)!=ModifierKeys.Alt){
e、 Target=null;
e、 已处理=正确;
}
}
}
}

根据以下链接:助记符在WPF中工作,无需按Alt键

线程中有关于在.NET4.0中对此进行更改的讨论,但这正是我测试它的内容,因此它似乎没有被更改


该线程描述了一种可能适合您的方法。

根据以下链接:助记符在WPF中工作,无需按Alt键

线程中有关于在.NET4.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 testBrowser
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();

      EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, 
                                        new AccessKeyPressedEventHandler(OnAccessKeyPressed));

      m_Browser.NavigateToString("<textarea cols=\"20\" rows=\"10\"/>");
    }

    private void m_Ok_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Ok pressed");
    }

    private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e)
    {    
      if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt) {
        e.Target = null;
        e.Handled = true;
      }
    }
  }
}