private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { // Restore the regular colors on the newly unselected item var previousSelection = _currentSelection; if (previousSelection != null) { previousSelection.Foreground = _currentSelectionUnselectedForeground; } // Remember the newly selected item's specified foreground color and then clear it to // allow the ControlTemplate to correctly set contrasting colors. _currentSelection = (TreeViewItem)treeView.SelectedItem; if (_currentSelection != null) { _currentSelectionUnselectedForeground = _currentSelection.Foreground; _currentSelection.ClearValue(TreeViewItem.ForegroundProperty); } }
public void GetMoreTree(RoutedEventArgs e) { TreeViewItem item = (TreeViewItem)e.OriginalSource; item.ClearValue(ItemsControl.ItemsSourceProperty); ObservableCollection <Element> observableCollection = new ObservableCollection <Element>(); Element fileInfo = (Element)item.DataContext; try { foreach (DirectoryInfo dir in new DirectoryInfo(fileInfo.FullName).GetDirectories().OrderBy(x => x.Name).ToArray()) { ShellObject shellFolder = ShellObject.FromParsingName(dir.FullName.ToString()); BitmapSource shellThumb = shellFolder.Thumbnail.SmallBitmapSource; Folder newItem = new Folder { Name = dir.ToString(), Icon = shellThumb, FullName = dir.FullName, }; try { newItem.CountOfFolders = new DirectoryInfo(dir.FullName).GetDirectories().Length.ToString(); } catch (UnauthorizedAccessException ex) { newItem.CountOfFolders = "Отказано в доступе"; } try { newItem.CountOfFiles = new DirectoryInfo(dir.FullName).GetFiles().Length.ToString(); } catch (UnauthorizedAccessException ex) { newItem.CountOfFiles = "Отказано в доступе"; } try { newItem.CreationTime = dir.CreationTime.ToString(CultureInfo.InvariantCulture); } catch (UnauthorizedAccessException ex) { } newItem.Children.Add(new Element { Name = "*", Icon = null, FullName = dir.FullName }); observableCollection.Add(newItem); } foreach (FileInfo file in new DirectoryInfo(fileInfo.FullName).GetFiles().OrderBy(x => x.Name).ToArray()) { System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.ExtractAssociatedIcon(file.FullName.ToString()); if (file.Extension.ToLower() == ".jpg" || file.Extension.ToLower() == ".jpeg" || file.Extension.ToLower() == ".bmp" || file.Extension.ToLower() == ".png") { observableCollection.Add(new CustomImage { Name = file.ToString(), Icon = icon.ToImageSource(), FullName = file.FullName.ToString(), Size = ByteSize.FromBytes(file.Length).ToString(), CreationTime = file.CreationTime.ToString(CultureInfo.InvariantCulture) }); } else { observableCollection.Add(new CustomFile { Name = file.ToString(), Icon = icon.ToImageSource(), FullName = file.FullName, Size = ByteSize.FromBytes(file.Length).ToString(), CreationTime = file.CreationTime.ToString(CultureInfo.InvariantCulture) }); } } } catch (UnauthorizedAccessException ex) { Folder newIFolder = new Folder() { Name = "Отказано в доступе" }; observableCollection.Add(newIFolder); } item.ItemsSource = observableCollection; }