private IHierarchyObject FirstChild(IHierarchyObject item) { if (item == null) { return(null); } return(item.GetAllChildren().FirstOrDefault()); }
private static ItemsControl BringObjectToView( ItemsControl parentItemsControl, IHierarchyObject parentDataObject, IHierarchyObject dataObject) { Debug.Assert(parentItemsControl != null); Debug.Assert(parentDataObject != null); Debug.Assert(dataObject != null); // Expand the the tree view item if necessary. var parentTreeViewItem = parentItemsControl as TreeViewItem; if (parentTreeViewItem != null) { if (!parentTreeViewItem.IsExpanded) { // TODO(rpaquay): Use ".IsExpanded = true"? parentTreeViewItem.SetValue(TreeViewItem.IsExpandedProperty, true); } } // Try to generate the ItemsPresenter and the ItemsPanel. by calling // ApplyTemplate. Note that in the virtualizing case even if the item is // marked expanded we still need to do this step in order to regenerate // the visuals because they may have been virtualized away. parentItemsControl.ApplyTemplate(); var itemsPresenter = (ItemsPresenter)parentItemsControl.Template.FindName("ItemsHost", parentItemsControl); if (itemsPresenter != null) { itemsPresenter.ApplyTemplate(); } else { // The Tree template has not named the ItemsPresenter, // so walk the descendents and find the child. itemsPresenter = FindVisualChild <ItemsPresenter>(parentItemsControl); if (itemsPresenter == null) { parentItemsControl.UpdateLayout(); itemsPresenter = FindVisualChild <ItemsPresenter>(parentItemsControl); if (itemsPresenter == null) { Logger.LogError("Can't find items presenter."); return(null); } } } // Access the custom VSP that exposes BringIntoView var virtualizingStackPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as VirtualizingStackPanel; if (virtualizingStackPanel == null) { Logger.LogError("Can't find virtual stack panel for parentItemsControl."); return(null); } var dataObjetIndex = parentDataObject.GetAllChildren().IndexOf(dataObject); if (dataObjetIndex >= parentItemsControl.Items.Count) { Logger.LogError("TreeView node has fewer parents than its corresponding data object."); return(null); } virtualizingStackPanel.BringIndexIntoViewPublic(dataObjetIndex); var treeViewItem = (ItemsControl)parentItemsControl.ItemContainerGenerator.ContainerFromIndex(dataObjetIndex); if (treeViewItem.DataContext != dataObject) { Logger.LogError("TreeView item data context is not the right data object."); return(null); } return(treeViewItem); }