[英]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.