2
Vote

[2.0.0634] Keyboard.FocusedElement isn't restored after reactivating the window

description

If you switch to another window and then activate the window that uses AvalonDock again, the Keyboard.FocusedElement is null because no keyboard focus is set (Keyboard.DefaultRestoreFocusMode = RestoreFocusMode.None in DockingManager).
 
Steps to reproduce:
 
  1. Start AvalonDock.TestApp and select some text in Document1
  2. Switch with alt+tab to other window and then back to AvalonDock.TestApp
  3. The keyboard focus isn't set to Document1. Keyboard.FocusedElement is null.

comments

adospace wrote May 12, 2012 at 7:22 PM

Fixed in latest release

dirkster wrote Aug 29, 2012 at 9:16 AM

Hi Ado,

I am still able to verify this ALT+TAB issue with AvalonDock 2.0 build 94905. I was unable to re-gain the document focus after tabbing away and back with ALT+TAB (as described above). I tested this in:

AvalonDock.MVVMTestApp
AvalonDock.TestApp

The selection is lost when I tab back to the AvalonDock app and the cursor is invisible.

dirkster wrote Sep 4, 2012 at 6:48 PM

I have finally found a workaround solution that works for me as long as I do not use an undocked document/tool window:

I subscripe to the Activated/deactivated event in the MainWindow class and make an attempt
at restoring the focus whenever the window gets activated (with ALT+TAB) for example. This
works in my sample app with AvalonEdit and in your MVVM Test App with the standard WPF
textbox.

I know this is most likely not what you would want to do eventually but at least the application
now starts to be a lot more useful - maybe you can add this into your sample app until we have
a better solution ...?

Thanks for your great work :) This is really cool to work with *-)

------------------------- Code Snippet --------------------------------------------
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = Workspace.This;

        this.Activated += new EventHandler(MainWindow_Activated);
        this.Deactivated += new EventHandler(MainWindow_Deactivated);
    }

    IInputElement RestoredFocus = null;
    void MainWindow_Deactivated(object sender, EventArgs e)
    {
      this.RestoredFocus = System.Windows.Input.Keyboard.FocusedElement;

      string FocusName = "(null)";
      string FocusType = "(null)";

      if (this.RestoredFocus != null)
      {
        try
        {
          System.Console.WriteLine("MainWindow_Deactivated Storing Focus Type: {0} Name: {1}", FocusType, FocusName);

          FocusName = (this.RestoredFocus as FrameworkElement).Name;
          FocusType = this.RestoredFocus.GetType().FullName;
        }
        catch
        {
        }
      }
    }

    void MainWindow_Activated(object sender, EventArgs e)
    {
      if (this.RestoredFocus != null)
      {
        try
        {
          this.RestoredFocus.Focus();
          System.Console.WriteLine("MainWindow_Activated restoring focus");
        }
        catch (Exception)
        {

          throw;
        }
      }
    }

dirkster wrote Sep 4, 2012 at 8:31 PM

I just found a better solution:

Just add

Keyboard.DefaultRestoreFocusMode = RestoreFocusMode.Auto;

into the constructor of the MainWindow after the this.InitializeComponent(); call and ALT+TAB works as it should.