[英]WPF WebBrowser fires button shortcut as ALT+Key is pressed
我对WPF有一个非常奇怪的问题。
一个简单的窗口,带有带快捷方式(“ _Ok”)和Webbrowser控件的按钮。 问题是:当光标按在Web浏览器内的文本区域中时,当我按“ 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>
以及后面的代码(只需设置浏览器内容):
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();
m_Browser.NavigateToString("<textarea cols=\"20\" rows=\"10\"/>");
}
private void m_Ok_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Ok pressed");
}
}
}
编辑:使用此代码修复:
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;
}
}
}
}
根据以下链接: AccessKey Pressed事件在WPF中无需ALT助记符而无需按Alt键的情况下按访问键时触发 。
线程中有人谈论过在.net 4.0中对此进行了更改,但是这就是我对其进行测试的内容,因此似乎没有。
该线程描述了一种可能对您有用的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.