我对WPF有一个非常奇怪的问题。
一个带有快捷键(“_Ok”)按钮和web浏览器控件的简单窗口。 问题是:当我按下“o”时光标位于webbrowser内的textarea中时,当我按下Alt + O时,wpf会触发按钮点击
这是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;
}
}
}
}
答案 0 :(得分:1)
根据以下链接:AccessKey Pressed Event Raised when pressing access key without ALT助记符在WPF中工作,无需按Alt键。
线程中有关于在.net 4.0中更改此内容的说法,但这是我测试它所以它似乎没有。
该主题描述了一种可能对您有用的方法。