从子窗口到父窗口的GotKeyboardFocus事件
本文关键字:窗口 GotKeyboardFocus 事件 | 更新日期: 2023-09-27 17:58:27
我有一个应用程序,每当某些UI元素(TextBox、PasswordBox等)获得焦点时,它必须打开屏幕键盘。我使用主窗口上的GotKeyboardFocus和LostKeyboard福克斯来实现这一点:
this.GotKeyboardFocus += AutoKeyboard.GotKeyboardFocus;
this.LostKeyboardFocus += AutoKeyboard.LostKeyboardFocus;
一切都很好,除了当我打开一个包含自己的TextBoxes的新窗口时。显然,由于它们不是MainWindows routedEvent的一部分,因此不会触发键盘焦点事件。有没有一种方法可以让所有子窗口从MainWindow继承GotKeyboardFocus,或者让它将键盘焦点事件传递回其父窗口?
我建议使用EventManager为所选事件注册全局(应用程序范围)处理程序。这里有一个例子:
public partial class App : Application
{
public App()
{
EventManager.RegisterClassHandler(
typeof (UIElement),
UIElement.GotKeyboardFocusEvent,
new RoutedEventHandler(GotKeyboardFocusEventHandler));
EventManager.RegisterClassHandler(
typeof (UIElement),
UIElement.LostKeyboardFocusEvent,
new RoutedEventHandler(LostKeyboardFocusEventHandler));
}
private void GotKeyboardFocusEventHandler(object sender, RoutedEventArgs routedEventArgs)
{
...
}
private void LostKeyboardFocusEventHandler(object sender, RoutedEventArgs routedEventArgs)
{
...
}
}