7

TabControl に異なる tabItems があり、各 tabItem にはいくつかの入力フィールドがあります。

プログラムで tabItems 間を移動しています (最初から次へ移動するウィザードのように)

「次へ」ボタン内でこのコードを使用しています

tabItem2.isSelected = true;

私の問題は、タブアイテムをクリックしてタブアイテム間を移動すると、フォーカス(キーボードフォーカス)が最初のテキストボックス入力に移動することです。

しかし、プログラム的に前のコードを使用すると、フォーカスは tabItem 内の最初の入力テキスト ボックス項目に移動しません。

何か案が?

4

2 に答える 2

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;
  }
}
于 2013-08-17T05:20:03.163 に答える