在WPF中以编程方式选择tabItem

时间:2009-06-01 23:07:56

标签: wpf tabcontrol tabitem

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

我正在以编程方式在tabItem之间移动(就像从第一个移动到下一个的向导)

我在“下一步”按钮

中使用此代码

tabItem2.isSelected = true;

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

但是以编程方式使用前面的代码,焦点不会移动到tabItem中的第一个输入文本框项。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

如果您强制使用IsSelected属性,我还会为第一个TextBox指定一个名称,并在设置所选选项卡后设置焦点。

如果您正在动态构建UI,这将无效,但您可以创建一个实用程序方法,在第一次输入时搜索逻辑树(如果您使用演示者/视图模型,则搜索可视树)控制然后设置焦点。

答案 1 :(得分: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;
  }
}
相关问题