2009-06-01 13 views
7

私はTabControl に異なるtabItemを持ち、各tabItemにはいくつかの入力フィールドがあります。WPFでtabItemをプログラムで選択

私が「次へ」ボタン

tabItem2.isSelected =真の内側このコードを使用してい

(次へ最初から移動するためのウィザードのような)プログラムtabItems間を移動しています。

私はtabItemsをクリックして移動すると、フォーカス(キーボードフォーカス)が最初のテキストボックスの入力に移動するという私の問題です。

前のコードでプログラムでは、フォーカスはtabItem内の最初の入力テキストボックス項目には移動しません。

+0

、あなたではなくタブコントロールよりも、フレームコントロールとページを使用して検討していますか?これはウィザードスタイルのUIに適しています。 –

+0

私が与えた答えは間違っていた! ( –

答えて

3

IsSelectedプロパティを強制している場合は、最初のTextBoxに名前を付けて、選択したタブを設定した後にフォーカスを設定します。

動的にUIを構築している場合、これは機能しませんが、論理ツリー(プレゼンター/ビューモデルを使用している場合はビジュアルツリー)を最初に検索するユーティリティメソッドを作成できますフォーカスを設定します。

+0

)textbox.Focus()を呼び出す代わりに、TabItem.OnPreviewGotKeyboardFocusメソッド内でWPFが内部的に行う処理を行うことができます。つまりtabitem.MoveFocus()を呼び出します。 – HappyNomad

0

これらのソリューションは私にとっては役に立たなかった。それは私が望むTabItemを遠くまで選択しましたが、目的のTreeViewItemを選択/フォーカスすることができませんでした。 (TabItemが既に選択されている場合は、TVIのみにフォーカスします)。下の解決策はついに私のために働きました。

(参考:以下のスニペットは、Microsoft Help Viewer 2.0に似たアプリの一部です。「同期」ボタンをクリックすると、まず選択されていない場合はコンテンツタブが選択され、一致するツリービュー項目を見つけた。それは、その後/焦点を選択する。)

乾杯

ただ、関心のうち
private void OnClick_SyncContents(object sender, RoutedEventArgs e) 
{ 
    // If the help-contents control isn't visible (ie., some other tab is currently selected), 
    // then use our common extension method to make it visible within the tab control. Once 
    // it visible, the extension method will call the event handler passed (which is this method) 
    if (!this.m_UcHelpFileContents.IsVisible) 
    { 
     this.m_UcHelpFileContents. 
     SelectParentTabItem_WaitForMeToBecomeVisible_ThenCallThisEventHandlerWithNullArguments 
     (this.OnClick_SyncContents); 
    } 
    else 
    { 
     // Else the help-contents control is currently visible, thus focus the 
     // matching tree view item 
     /* Your code here that focuses the desired tree view item */ 
    } 
} 


public static class CommonExtensionMethods 
{ 
    public static void 
    SelectParentTabItem_WaitForMeToBecomeVisible_ThenCallThisEventHandlerWithNullArguments 
    (this FrameworkElement frameworkElement, RoutedEventHandler eventHandlerToCallWhenVisible) 
    { 
    // First, define the handler code for when the given framework element becomes visible 
    DependencyPropertyChangedEventHandler HANDLER = null; 
    HANDLER = (s, e) => 
    { 
     // If here, the given framework element is now visible and its tab item currently selected 
     // Critical: first and foremost, undo the latch to is-visible changed 
     frameworkElement.IsVisibleChanged -= HANDLER; 

     // Now invoke the event handler that the caller wanted to invoke once visible 
     frameworkElement.Dispatcher.BeginInvoke(eventHandlerToCallWhenVisible, null, null); 
    }; 

    // Use our common extension method to find the framework element's parent tab item 
    TabItem parentTabItem = frameworkElement.GetFirstParentOfType<TabItem>(); 

    if (parentTabItem != null) 
    { 
     // Assign the handler to the given framework element's is-visible-changed event 
     frameworkElement.IsVisibleChanged += HANDLER; 

     // Now set the tab item's is-selected property to true (which invokes the above 
     // handler once visible) 
     parentTabItem.IsSelected = true; 
    } 
    } 


    public static T GetFirstParentOfType<T> 
    (this FrameworkElement frameworkElement) where T : FrameworkElement 
    { 
    for (FrameworkElement fe = frameworkElement.Parent as FrameworkElement; 
     fe != null; 
     fe = fe.Parent as FrameworkElement) 
    { 
     if (fe is T) 
     return fe as T; 
    } 

    // If here, no match 
    return null; 
    } 
} 
関連する問題