public TemplateTheTree() { Title = "Template the Tree"; TreeView tree = new TreeView(); Content = tree; HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(DiskDirectory)); template.ItemsSource = new Binding("Subdirectories"); FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(TextBlock)); factoryTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Name")); template.VisualTree = factoryTextBlock; DiskDirectory dir = new DiskDirectory(new DirectoryInfo(Path.GetPathRoot(Environment.SystemDirectory))); TreeViewItem item = new TreeViewItem(); item.Header = dir.Name; item.ItemsSource = dir.Subdirectories; item.ItemTemplate = template; tree.Items.Add(item); item.IsExpanded = true; }
public TemplateTheTree() { Title = "Template the Tree"; // Create TreeView and set as content of window. TreeView treevue = new TreeView(); Content = treevue; // Create HierarchicalDataTemplate based on DiskDirectory. HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(DiskDirectory)); // Set Subdirectories property as ItemsSource. template.ItemsSource = new Binding("Subdirectories"); // Create FrameworkElementFactory for TextBlock. FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(TextBlock)); // Bind Text property with Name property from DiskDirectory. factoryTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Name")); // Set this Textblock as the VisualTree of the template. template.VisualTree = factoryTextBlock; // Create a DiskDirectory object for the system drive. DiskDirectory dir = new DiskDirectory( new DirectoryInfo( Path.GetPathRoot(Environment.SystemDirectory))); // Create a root TreeViewItem and set its properties. TreeViewItem item = new TreeViewItem(); item.Header = dir.Name; item.ItemsSource = dir.Subdirectories; item.ItemTemplate = template; // Add TreeViewItem to TreeView. treevue.Items.Add(item); item.IsExpanded = true; }
public TemplateTheTree() { Title = "Template the Tree"; // Create TreeView and set as content of window. TreeView treevue = new TreeView(); Content = treevue; // Create HierarchicalDataTemplate based on DiskDirectory. HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(DiskDirectory)); // Set Subdirectories property as ItemsSource. template.ItemsSource = new Binding("Subdirectories"); // Create FrameworkElementFactory for TextBlock. FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(TextBlock)); // Bind Text property with Name property from DiskDirectory. factoryTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Name")); // Set this Textblock as the VisualTree of the template. template.VisualTree = factoryTextBlock; // Create a DiskDirectory object for the system drive. DiskDirectory dir = new DiskDirectory( new DirectoryInfo( Path.GetPathRoot(Environment.SystemDirectory))); // Create a root TreeViewItem and set its properties. TreeViewItem item = new TreeViewItem(); item.Header = dir.Name; item.ItemsSource = dir.Subdirectories; item.ItemTemplate = template; // Add TreeViewItem to TreeView. treevue.Items.Add(item); item.IsExpanded = true; }