mercredi 1 avril 2015

Custom WinForm UserControl inside WindowsFormHost can't focus properly

My WPF Project contains a view (UserControl) and this view contains a WindowsFormHost containing a custom WinForm UserControl.


When I am navigating to this view, I want to have the keyboard focus directly into the WinForm, but I must click on it to gain the control (which is not the intended behavior).


I have investigated with Snoop and it's my Windows that contains the focus and when I click the winForm gains the focus. I have tried a lot of different things, but now this code below seems to work, but only the first time. If I navigate to the menu and go back in the game, the code is still triggered, but the focus methods has no effects.


How can I gain the focus on the WinForm control without clicking on it (and does not require to recreate a view each time)?





private void GameControl_OnGameReady(object sender, EventArgs e)
{
if (!this.Dispatcher.HasShutdownStarted)
this.Dispatcher.Invoke(() =>
{
for (int i = 0; i < 5; ++i)
{
this.Host.Focus();
this.Host.TabInto(new TraversalRequest(FocusNavigationDirection.First));
this.GameControl.Focus(); //Child of the Host
Console.WriteLine("TEST");
}

});
}



Aucun commentaire:

Enregistrer un commentaire