【问题标题】:select tabItem programmatically in WPF在 WPF 中以编程方式选择 tabItem
【发布时间】:2009-06-01 23:07:55
【问题描述】:

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

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

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

tabItem2.isSelected = true;

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

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

有什么想法吗?

【问题讨论】:

  • 只是出于兴趣,您是否考虑过使用 Frame 控件和 Pages,而不是 TabControl?它更适合向导风格的 UI。
  • 我给出的答案是错误的! :(

标签: wpf tabcontrol tabitem


【解决方案1】:

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

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

【讨论】:

  • 或者代替调用 textbox.Focus(),您可以执行 WPF 在其 TabItem.OnPreviewGotKeyboardFocus 方法内部执行的操作。即调用tabitem.MoveFocus()。
【解决方案2】:

这些解决方案对我不起作用。它已经选择了我想要的 TabItem,但它无法选择/聚焦所需的 TreeViewItem。 (如果已经选择了 TabItem,它只会关注 TVI。)下面的解决方案最终对我有用。

(仅供参考:下面的 sn-ps 是类似于 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;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2010-11-07
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多