2009-07-28 6 views
2

J'ai une fenêtre WPF complexe avec un TabControl. L'un des TabItems héberge un WindowsFormsHost, qui héberge certains anciens contrôles Windows Forms. Lorsque j'accède à cet onglet, j'essaie de mettre l'accent sur l'un de ces contrôles. Keyboard.Focus() ne fonctionne pas car il nécessite un IInputElement, que les anciens contrôles de formulaires Windows ne prennent pas en charge. Ainsi, j'appelle la méthode Focus() de l'ancien Windows Forms control, mais pour une raison quelconque, cela ne fonctionne pas.Définition du focus pour un élément Windows Forms hébergé dans WPF

je mets le code pour appeler la mise au point() sur chaque événement que vous pouvez penser:

  1. événement SelectionChanged de TabControl
  2. l'événement IsVisibleChanged de TabItem
  3. l'événement GotFocus de TabItem

Aucun d'entre eux travaillent. Quelqu'un a des idées?

Merci

Répondre

2

Ma solution:

private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
{ 
    Dispatcher.BeginInvoke(() => 
     { 
      FormsControl.Focus(); 
      System.Windows.Input.Keyboard.Focus(ControlHost); 
      Dispatcher.BeginInvoke(() => FormsControl.Focus()); 
     });   
} 

public static class DispatcherExtensions 
{ 
    /// <summary> 
    /// Executes the specified delegate asynchronously on the thread the System.Windows.Threading.Dispatcher is associated with. 
    /// </summary> 
    /// <param name="dispatcher">System.Windows.Threading.Dispatcher</param> 
    /// <param name="a">A delegate to a method that takes no arguments and does not return a value, which is pushed onto the System.Windows.Threading.Dispatcher event queue.</param> 
    /// <returns>An object, which is returned immediately after Overload:System.Windows.Threading.Dispatcher.BeginInvoke is called, that represents the operation that has been posted to the System.Windows.Threading.Dispatcher queue.</returns> 
    public static DispatcherOperation BeginInvoke(this Dispatcher dispatcher, Action a) 
    { 
     return dispatcher.BeginInvoke(a, null); 
    } 
} 
Questions connexes