다음 이벤트는 사용할 수 있지만, 그들은 각 요소에 대해 첨부해야합니다 : 포커스 요소가 변경되었을 경우 감지 전역하는 .NET WPF의 방법이 있나요,키보드 포커스 이벤트를 전역 적으로 감지 할 수 있습니까?
GotKeyboardFocus
LostKeyboardFocus
를? 가능한 모든 요소에 이벤트 리스너를 추가 할 필요없이?다음 이벤트는 사용할 수 있지만, 그들은 각 요소에 대해 첨부해야합니다 : 포커스 요소가 변경되었을 경우 감지 전역하는 .NET WPF의 방법이 있나요,키보드 포커스 이벤트를 전역 적으로 감지 할 수 있습니까?
GotKeyboardFocus
LostKeyboardFocus
를? 가능한 모든 요소에 이벤트 리스너를 추가 할 필요없이?당신은 tunneling 미리보기 이벤트에 연결할 수 : 자손 중 하나가 도착 또는 키보드 포커스를 잃을 때
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525"
PreviewGotKeyboardFocus="Window_PreviewGotKeyboardFocus"
PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus">
....
이 방법은 위 그림과 같이 창은 모든 자손 전에 통지 할 것입니다.
자세한 내용은 this을 참조하십시오.
메인 윈도우에 라우트 된 이벤트 핸들러를 추가하고 처리 된 이벤트에 관심이 있다고 지정할 수 있습니다.
mainWindow.AddHandler(
UIElement.GotKeyboardFocusEvent,
OnElementGotKeyboardFocus,
true
);
완벽하게 작동했습니다! 많은 감사합니다! – Alexey
당신이 어떤 클래스에서이 작업을 수행 할 수 있습니다 : 그들은 InputManager.PostProcessInput
이벤트에 가입 :
//In the constructor
EventManager.RegisterClassHandler(
typeof(UIElement),
Keyboard.PreviewGotKeyboardFocusEvent,
(KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
...
private void OnPreviewGotKeyboardFocus(object sender,
KeyboardFocusChangedEventArgs e)
{
// Your code here
}
초점 변경하면 어떻게 마이크로 소프트의 트리거 CommandManager.RequerySuggested
이벤트에서보세요.
간단한 예 :
이것은 또한 멀티 윈도우 응용 프로그램에서 작동static KeyboardControl()
{
InputManager.Current.PostProcessInput += InputManager_PostProcessInput;
}
static void InputManager_PostProcessInput(object sender, ProcessInputEventArgs e)
{
if (e.StagingItem.Input.RoutedEvent == Keyboard.GotKeyboardFocusEvent ||
e.StagingItem.Input.RoutedEvent == Keyboard.LostKeyboardFocusEvent)
{
KeyboardFocusChangedEventArgs focusArgs = (KeyboardFocusChangedEventArgs)e.StagingItem.Input;
KeyboardControl.IsOpen = focusArgs.NewFocus is TextBoxBase;
}
}
.
이 방법은 미리보기 이벤트이므로 실제로 포커스가 변경되지 않을 수도 있으므로 잘못된 결과가 발생할 수 있습니다. 이것은 윈도우와 소스 요소 사이의 UIElement가 이벤트를 처리 대상으로 표시 한 경우 발생합니다. – Hank
나는 @Hank에 동의한다. PreviewXxx는 실제로 변경 될 것이라고 보장하지 않습니다. Vaccano의 대답은 실제로 올바른 것입니다. Nicolas에도 대체품이 있지만, 처리 된 이벤트로 인해 체인에있는 모든 사람에게 알려주기 때문에 완전히 동일하지는 않습니다. – MarqueIV