如何在对话框之后设置焦点控件?

发布于 2024-12-09 02:46:05 字数 216 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁人与我共长歌 2024-12-16 02:46:05

我发现了一个可行的黑客方法,尽管我不喜欢它,因为我觉得它将我的视图与我的 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 a MessageBox error and reset the IsFocused property afterwards so it updates the UI. Last change made was to update my ViewModels to publish errors to the EventAggregator instead of handling themselves with a MessageBox, which is probably better anyways.

I suppose it works, even if I don't like it

娇妻 2024-12-16 02:46:05

不确定这是否对您的情况有帮助,但在我的情况下,我可以将焦点设置回主窗口,这可以通过以下方式完成:

App.Current.MainWindow.Focus();

只需确保主窗口已正确初始化,如果出现飞溅,情况可能并非如此屏幕或某些登录窗口或其他东西最初占据了主窗口角色(即通过 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

App.Current.MainWindow.Focus();

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.

假装爱人 2024-12-16 02:46:05
using System.Runtime.InteropServices;
using System.Windows.Interop;

public class Interop
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

public static IntPtr GetWindowHandle(Window window)
{
     return new WindowInteropHelper(window).Handle;
}
}

// In main window, when the MessageBox is closed
IntPtr window = Interop.GetWindowHandle(this);
IntPtr focused = Interop.GetForegroundWindow();
if (window != focused)
{ 
    Interop.SetForegroundWindow(window);
}

http://tech.avivo.si/2009/11/how-to-focus-window-in-wpf-when-it-gets-out-of-focus/

using System.Runtime.InteropServices;
using System.Windows.Interop;

public class Interop
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

public static IntPtr GetWindowHandle(Window window)
{
     return new WindowInteropHelper(window).Handle;
}
}

// In main window, when the MessageBox is closed
IntPtr window = Interop.GetWindowHandle(this);
IntPtr focused = Interop.GetForegroundWindow();
if (window != focused)
{ 
    Interop.SetForegroundWindow(window);
}

http://tech.avivo.si/2009/11/how-to-focus-window-in-wpf-when-it-gets-out-of-focus/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文