2009-07-28 24 views
2

TabControlを使用した複雑なWPFウィンドウがあります。 TabItemの1つは、古いWindowsフォームコントロールをホストするWindowsFormsHostをホストします。WPFでホストされているWindowsフォーム要素のフォーカスを設定する

このタブに移動すると、これらのコントロールの1つにフォーカスを設定しようとします。 古いWindowsフォームコントロールでサポートされていないIInputElementが必要なため、Keyboard.Focus()が機能しません。だから、私は古いWindowsフォームコントロール自体のFocus()メソッドを呼び出すことになっていますが、何らかの理由でそれが機能していません。

  1. のTabControlのSelectionChangedイベント
  2. のTabItemのIsVisibleChangedイベント
  3. のTabItemのGotFocusイベント

なし:

は、私はあなたが考えることができるすべてのイベントにフォーカスを()を呼び出すコードを置きます彼らの仕事。誰にもアイデアはありますか?

おかげ

答えて

2

私のソリューション:

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); 
    } 
} 
関連する問題