Ejemplo n.º 1
0
 public TreeViewItem FindInTree(TreeView treeView)
 {
     treeView.UpdateLayout();
     TreeViewItem treeViewItem = null;
     var itemContainerGenerator = treeView.ItemContainerGenerator;
     var path = GetPathToRoot();
     path.Reverse();
     foreach (var item in path)
     {
         treeViewItem = (TreeViewItem) itemContainerGenerator.ContainerFromItem(item);
         if (treeViewItem == null) return null;
         itemContainerGenerator = treeViewItem.ItemContainerGenerator;
     }
     return treeViewItem;
 }
        /// <summary>
        /// Recursively loops through the treeview structure to get the correct
        /// folder with the right path
        /// </summary>
        /// <param name="targetPath">Desired path</param>
        /// <param name="tvItem">Current treeview item</param>
        private static void RecursivelySelectFolder(TreeView treeView, string targetPath, TreeViewItem tvItem)
        {
            Folder thisFolder = tvItem.DataContext as Folder;

            // We found this item - select it
            if (targetPath.Equals(thisFolder.Path, StringComparison.CurrentCultureIgnoreCase) ||
                targetPath.Equals(thisFolder.Path + "\\", StringComparison.CurrentCultureIgnoreCase))
            {
                tvItem.IsSelected = true;
                tvItem.BringIntoView();
            }
            // We're on the right path, keep moving down a level
            else if (targetPath.StartsWith(thisFolder.Path + (thisFolder.IsDrive ? "" : "\\"), StringComparison.CurrentCultureIgnoreCase))
            {
                thisFolder.ReadSubItemsForFolder();
                tvItem.IsExpanded = true;
                treeView.UpdateLayout();

                foreach (Folder subFolder in tvItem.Items)
                {
                    TreeViewItem subItem = tvItem.ItemContainerGenerator.ContainerFromItem(subFolder) as TreeViewItem;
                    RecursivelySelectFolder(treeView, targetPath, subItem);
                }
            }
            else // This isn't it, break the tree
            {
                tvItem.IsExpanded = false;
                treeView.UpdateLayout();
                return;
            }
        }