Populate TreeView with file system directory structure

Option #1: Recursive approach: private void ListDirectory(TreeView treeView, string path) { treeView.Nodes.Clear(); var rootDirectoryInfo = new DirectoryInfo(path); treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo)); } private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo) { var directoryNode = new TreeNode(directoryInfo.Name); foreach (var directory in directoryInfo.GetDirectories()) directoryNode.Nodes.Add(CreateDirectoryNode(directory)); foreach (var file in directoryInfo.GetFiles()) directoryNode.Nodes.Add(new TreeNode(file.Name)); return directoryNode; } Option #2: Non-recursive approach: private static void ListDirectory(TreeView treeView, … Read more

How to get TreeViewItem from HierarchicalDataTemplate item?

TreeViewItem item = (TreeViewItem)(mainTreeList .ItemContainerGenerator .ContainerFromIndex(mainTreeList.Items.CurrentPosition)); DOES NOT WORK (for me) as mainTreeList.Items.CurrentPosition in a treeview using a HierarchicalDataTemplate will always be -1. NEITHER DOES below as as mainTreeList.Items.CurrentItem in a treeview using a HierarchicalDataTemplate will always be null. TreeViewItem item = (TreeViewItem)mainTreeList .ItemContainerGenerator .ContainerFromItem(mainTreeList.Items.CurrentItem); INSTEAD I had to set a the last selected TreeViewItem … Read more

WPF Treeview Databinding Hierarchal Data with mixed types

Since you want the elements in the TreeView to have a list of children that consists of both Categories Products, you will want your Category ViewModel to have a collection that consists of both Categories and Products. For example, you could use a CompositeCollection to combine your existing collections: public class Category { public string … Read more

WPF MVVM TreeView SelectedItem

You should not really need to deal with the SelectedItem property directly, bind IsSelected to a property on your viewmodel and keep track of the selected item there. A sketch: <TreeView ItemsSource=”{Binding TreeData}”> <TreeView.ItemContainerStyle> <Style TargetType=”{x:Type TreeViewItem}”> <Setter Property=”IsSelected” Value=”{Binding IsSelected}” /> </Style> </TreeView.ItemContainerStyle> </TreeView> public class TViewModel : INotifyPropertyChanged { private static object _selectedItem … Read more

How to avoid winforms treeview icon changes when item selected

Simply set the SelectedImageIndex for each node to the same value as ImageIndex. So, if you’re creating your node programatically: TreeNode node = new TreeNode(“My Node”); node.ImageIndex = 1; node.SelectedImageIndex = 1; Or you can specify the whole lot in the constructor: TreeNode node = new TreeNode(“My Node”, 1, 1); You can do the same … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)