2009-06-01 83 views
7

我有不同的TabItems在一個TabControl 每個TabItem的有一些輸入字段。選擇TabItem的編程方式在WPF

我的的TabItems之間移動編程(類似於嚮導從第一位置移動到下一個)

我使用的「下一步」按鈕

tabItem2.isSelected =真這裏面的代碼;

我的問題是,當我點擊他們的TabItems之間移動,對焦(鍵盤焦點)將移到第一個文本框輸入。

但是編程方式與前面的代碼,焦點不會移動到的TabItem內的第一個文本框輸入項目。

有什麼想法?

+0

只是出於興趣,紛紛您使用幀控制和網頁,而不是一個TabControl考慮?它更適合嚮導式用戶界面。 – 2009-06-01 23:10:23

+0

我給的答案是錯的! :( – 2010-01-25 05:45:38

回答

3

如果你強迫IsSelected屬性,我也想給第一個TextBox的名稱並設置焦點設置選定的選項卡後。

如果你動態地構建你的UI,這是不行的,但你可以創建搜索邏輯樹的實用工具方法(或可視化樹,如果你正在使用主持人/視圖模型)的第輸入控件然後設置焦點。

0

這些解決方案並沒有爲我工作。它儘可能選擇我想要的TabItem,但它無法選擇/聚焦所需的TreeViewItem。 (如果已經選擇了TabItem,它將只關注TVI。)下面的解決方案最終爲我工作。當您點擊「同步」按鈕時,它將首先選擇「內容」選項卡(如果尚未選擇),然後遍歷到樹形視圖中,直到它出現爲止。找到匹配的樹視圖項。它然後選擇/對焦。)

乾杯

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; 
    } 
}