2

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

Rachel
  • 130,264
  • 66
  • 304
  • 490

3 Answers3

0

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.

jxramos
  • 7,356
  • 6
  • 57
  • 105
0

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

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
-1
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/

Joe Mancuso
  • 2,099
  • 1
  • 16
  • 17
  • Unfortunately I don't think this will work. My MessageBoxes are raised from the ViewModel, not my View, and I am working with an XBAP and not a Desktop Application, so I am using `Page` objects, not `Window` objects – Rachel Oct 10 '11 at 17:22
  • Depending on how you are calling it from your ViewModel, your MessageBox should have a pointer to its parent window (even if that is the IE window). You might check that angle. I do similar things, but the framework that I use (Cinch) enables me to grab the View object when necessary. I would then cast it to a Framework element and call Focus(). – Joe Mancuso Oct 10 '11 at 17:31