private bool ExpandAndSelectItem(ItemsControl parentContainer, object itemToSelect) { foreach (Object item in parentContainer.Items) { count++; TreeListViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeListViewItem; if (item == itemToSelect && currentContainer != null) { currentContainer.IsSelected = true; currentContainer.BringIntoView(); currentContainer.Focus(); ScrollViewer viewer = VisualUpwardSearch <ScrollViewer>(currentContainer as DependencyObject) as ScrollViewer; viewer.ScrollToVerticalOffset(count * 25); return(true); } if (currentContainer != null && currentContainer.Items.Count > 0) { bool wasExpanded = currentContainer.IsExpanded; currentContainer.IsExpanded = true; currentContainer.UpdateLayout(); if (ExpandAndSelectItem(currentContainer, itemToSelect)) { return(true); } else { currentContainer.IsExpanded = wasExpanded; currentContainer.UpdateLayout(); } } } return(false); }
private TreeListViewItem FindTreeViewItem(ItemsControl item, object data) { TreeListViewItem findItem = null; bool itemIsExpand = false; if (item is TreeListViewItem) { TreeListViewItem tviCurrent = item as TreeListViewItem; object itemData = tviCurrent.DataContext; if (itemData == data) { findItem = tviCurrent; } else { itemIsExpand = tviCurrent.IsExpanded; if (!tviCurrent.IsExpanded) { tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, true); tviCurrent.UpdateLayout(); } } } if (findItem == null) { for (int i = 0; i < item.Items.Count; i++) { TreeListViewItem tvItem = (TreeListViewItem)item.ItemContainerGenerator.ContainerFromIndex(i); if (tvItem == null) { continue; } object itemData = item.Items[i]; if (itemData == data) { findItem = tvItem; break; } else if (tvItem.Items.Count > 0) { findItem = FindTreeViewItem(tvItem, data); if (findItem != null) { break; } } } TreeListViewItem tviCurrent = item as TreeListViewItem; tviCurrent.SetValue(TreeListViewItem.IsExpandedProperty, itemIsExpand); tviCurrent.UpdateLayout(); } return(findItem); }
public void SetScorllViewPosition(object item, object oneLevelTree = null) { if (item == null) { return; } TreeListViewItem container = null; if (oneLevelTree == null) { foreach (var tree in this.Items) { TreeListViewItem tlvi = this.ItemContainerGenerator.ContainerFromItem(tree) as TreeListViewItem; container = FindTreeViewItem(tlvi, item) as TreeListViewItem; if (container != null) { break; } } if (container == null) { return; } container.BringIntoView(); container.IsSelected = true; } else { TreeListViewItem tlvi = this.ItemContainerGenerator.ContainerFromItem(oneLevelTree) as TreeListViewItem; if (item == oneLevelTree) { tlvi.IsSelected = true; tlvi.BringIntoView(); tlvi.Focus(); } else { tlvi.IsExpanded = true; tlvi.UpdateLayout(); count = 0; foreach (Object itemTree in this.Items) { count++; if (itemTree == oneLevelTree) { break; } } ExpandAndSelectItem(tlvi, item); } } }