Ces solutions n'ont pas fonctionné pour moi. Il a été aussi loin de sélectionner le TabItem que je voulais, mais il n'était pas capable de sélectionner/focuser le TreeViewItem désiré. (Cela ne ferait que focaliser le TVI si le TabItem était déjà sélectionné.) La solution ci-dessous a finalement fonctionné pour moi.
(Les extraits ci-dessous font partie d'une application similaire à Microsoft Help Viewer 2.0.) Lorsque vous cliquez sur le bouton "Sync", elle sélectionne d'abord l'onglet Contenu si elle n'est pas déjà sélectionnée. trouve l'élément de l'arborescence correspondant. ce qui sélectionne/concentre ensuite.)
Vive
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;
}
}
Juste d'intérêt, vous avez envisagé d'utiliser le contrôle Frame et des pages, au lieu d'un TabControl? C'est beaucoup mieux adapté pour les interfaces utilisateur de style Assistant. –
la réponse que j'ai donnée était fausse! :( –