Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window Focus problem with .NET 4.0 and WindowsFormstHost

I'm getting some strange behavior that I'm having trouble with:

  1. Add a simple Forms control with text box to a WindowsFormsHost;
  2. Add a button that opens another WPF Window (not setting owner);
  3. Maximize the original WPF window and click on the text box so it has focus;
  4. When you do that and then try to activate the other WPF window from the task bar it gets activated and then deactivated.

What is troubling is that if I compile this under .NET 3.5 it works no problem.

Any ideas?

like image 796
AKoran Avatar asked Jan 05 '11 22:01

AKoran


1 Answers

Implementing this on a derived window helps a bit - but does not solve the problem completely:

protected override void OnDeactivated(EventArgs e)
{
  var host = System.Windows.Input.FocusManager.GetFocusedElement(this) as System.Windows.Forms.Integration.WindowsFormsHost;
  if (host !=null )
  {
    Focus();
  }

  base.OnDeactivated(e);
}

It sets focus to the window on deactivate - this is not early enough so activation flips back, but when you alt-tab second time it switches. Focused control is not preserved though. I tried dispatching focus set - but no good.

like image 87
Rune Andersen Avatar answered Dec 28 '22 19:12

Rune Andersen