2009-06-01 14 views
7

Tôi có các tabItem khác nhau trong TabControl và mỗi tabItem có một số trường nhập.select tabItem lập trình trong WPF

Tôi đang di chuyển giữa các tabItems lập trình (giống như một hướng dẫn để di chuyển từ người đầu tiên tiếp theo)

Tôi đang sử dụng mã này bên trong nút "Next"

tabItem2.isSelected = true;

vấn đề của tôi khi tôi di chuyển giữa tabItems bằng cách nhấp vào chúng, tiêu điểm (tiêu điểm bàn phím) sẽ di chuyển đến đầu vào hộp văn bản đầu tiên.

Nhưng lập trình với mã trước đó, tiêu điểm sẽ không di chuyển đến mục hộp văn bản đầu vào đầu tiên bên trong tabItem.

Bất kỳ ý tưởng nào?

+0

Chỉ cần ra quan tâm, có bạn xem là sử dụng điều khiển khung và Pages, chứ không phải là một TabControl? Nó phù hợp hơn với giao diện người dùng kiểu thuật sĩ. –

+0

câu trả lời tôi đưa ra là sai! :( –

Trả lời

3

Nếu bạn đang buộc thuộc tính IsSelected, tôi cũng đặt tên cho hộp văn bản đầu tiên và đặt tiêu điểm sau khi bạn đặt tab đã chọn.

Nếu bạn đang xây dựng giao diện người dùng động, điều này sẽ không hoạt động, nhưng bạn có thể tạo phương thức tiện ích tìm kiếm cây hợp lý (hoặc cây thị giác nếu bạn đang sử dụng người trình bày/chế độ xem) điều khiển đầu vào và sau đó đặt tiêu điểm.

+0

Hoặc thay vì gọi textbox.Focus(), bạn có thể làm những gì WPF thực hiện trong nội bộ phương thức TabItem.OnPreviewGotKeyboardFocus của nó, gọi là tabitem.MoveFocus(). – HappyNomad

0

Các giải pháp này không hiệu quả đối với tôi. Nó đã chọn được TabItem mà tôi muốn, nhưng nó không thể chọn/lấy tiêu điểm TreeViewItem mong muốn. (Nó sẽ chỉ tập trung vào TVI nếu TabItem đã được chọn.) Giải pháp dưới đây cuối cùng cũng làm việc cho tôi.

(FYI: Đoạn trích bên dưới là một phần của ứng dụng tương tự như Microsoft Help Viewer 2.0. Khi bạn nhấp vào nút "Đồng bộ hóa", trước tiên nó sẽ chọn tab Nội dung nếu chưa được chọn, sau đó chuyển sang chế độ xem dạng cây cho đến khi tìm thấy mục xem cây phù hợp. Mà nó sau đó chọn/tập trung.)

Cheers

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