如何在对话框之后设置焦点控件?
我正在开发一个 XBAP 应用程序,用户主要使用键盘进行导航。当我显示 MessageBox
时,我可以按 Enter 键将其关闭,但主应用程序似乎没有重新获得焦点。我必须手动在屏幕上单击鼠标才能将焦点放回到应用程序上。
有办法解决这个问题吗?
编辑
我可以验证应用程序仍然具有逻辑焦点,但它只是没有键盘焦点
I'm working on an XBAP app where Users primarily use the Keyboard for Navigation. When I display a MessageBox
, I can hit Enter to close it but then the main application doesn't seem to regain focus. I have to manually click the mouse on the screen to put focus back on the application.
Is there a way around this?
Edit
I can verify that the app still has Logical Focus, but it just doesn't have Keyboard Focus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现了一个可行的黑客方法,尽管我不喜欢它,因为我觉得它将我的视图与我的 ViewModel 联系起来
我正在使用 IsFocused AttachedProperty 将控件绑定到View 背后的布尔属性。同一个视图还订阅了
DisplayError
事件,该事件显示MessageBox
错误并随后重置IsFocused
属性,以便更新 UI。最后的更改是更新我的 ViewModel 以将错误发布到 EventAggregator,而不是使用 MessageBox 来处理自身,无论如何,这可能更好。我想它会起作用,即使我不喜欢它
I found a hack that works, although I don't like it because I feel it ties my Views to my ViewModel
I'm using an IsFocused AttachedProperty to bind a control to a boolean property behind the View. The same View is also subscribing to a
DisplayError
event that displays aMessageBox
error and reset theIsFocused
property afterwards so it updates the UI. Last change made was to update my ViewModels to publish errors to theEventAggregator
instead of handling themselves with aMessageBox
, which is probably better anyways.I suppose it works, even if I don't like it
不确定这是否对您的情况有帮助,但在我的情况下,我可以将焦点设置回主窗口,这可以通过以下方式完成:
只需确保主窗口已正确初始化,如果出现飞溅,情况可能并非如此屏幕或某些登录窗口或其他东西最初占据了主窗口角色(即通过 StartupUri),然后此后没有更新任何其他内容。
这对我有用,因为我在主窗口级别处理所有键盘事件以驱动视图模型的更新。
Not sure if this will help your situation but in my circumstance it was ok for me to set focus back to main window, which was able to be accomplished with
Just be sure main window is properly initialized, which may not be the case if a splash screen or some login window or something initially grabbed the main window role (ie by StartupUri) and then nothing else was updated thereafter.
This worked for me since I was handling all keyboard events at the main window level to drive updates to my view model.
http://tech.avivo.si/2009/11/how-to-focus-window-in-wpf-when-it-gets-out-of-focus/
http://tech.avivo.si/2009/11/how-to-focus-window-in-wpf-when-it-gets-out-of-focus/