在WPF中以编程方式选择tabItem

在WPF中以编程方式选择tabItem,wpf,tabcontrol,tabitem,Wpf,Tabcontrol,Tabitem,我在TabControl中有不同的tabItems 每个tabItem都有一些输入字段 我以编程方式在选项卡项之间移动,就像向导一样从第一个移动到下一个 我在“下一步”按钮中使用此代码 tabItem2.isSelected=true 我的问题是,当我通过单击选项卡项在它们之间移动时,焦点键盘焦点将移动到第一个文本框输入 但是通过前面的代码编程,焦点不会移到tabItem中的第一个输入文本框项 有什么想法吗?如果您强制使用IsSelected属性,我也会给第一个文本框命名,并在设置所选选项卡后设

我在TabControl中有不同的tabItems 每个tabItem都有一些输入字段

我以编程方式在选项卡项之间移动,就像向导一样从第一个移动到下一个

我在“下一步”按钮中使用此代码

tabItem2.isSelected=true

我的问题是,当我通过单击选项卡项在它们之间移动时,焦点键盘焦点将移动到第一个文本框输入

但是通过前面的代码编程,焦点不会移到tabItem中的第一个输入文本框项


有什么想法吗?

如果您强制使用IsSelected属性,我也会给第一个文本框命名,并在设置所选选项卡后设置焦点


如果您要动态构建UI,这将不起作用,但是如果您使用演示者/视图模型作为第一个输入控件,您可以创建一个实用方法来搜索逻辑树或视觉树,然后设置焦点。

这些解决方案对我不起作用。它选择了我想要的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;
  }
}

出于兴趣,您是否考虑过使用Frame控件和Pages,而不是TabControl?它更适合于向导式UI。我给出的答案是错误的!:或者不调用textbox.Focus,您可以在WPF的TabItem.OnPreviewGotKeyboardFocus方法内部执行WPF所做的操作。也就是说,调用tabitem.MoveFocus。