TreeView, HierarchicalDataTemplate and recursive Data

You should only have to declare the HierarchicalDataTemplate for NodeViewModel as this is the only thing showing in the TreeView, and bind the actual ItemSource to the TreeView <TreeView ItemsSource=”{Binding Items}”> <TreeView.Resources> <HierarchicalDataTemplate DataType=”{x:Type local:NodeViewModel}” ItemsSource=”{Binding Children}”> <TextBlock Text=”{Binding Name}”></TextBlock> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> Full Example Xaml: <Window x:Class=”WpfApplication13.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:local=”clr-namespace:WpfApplication13″ Title=”MainWindow” x:Name=”UI” Width=”343″ Height=”744.625″ … 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

Find node clicked under context menu

You can add a mouse click event to the TreeView, then select the correct node using GetNodeAt given the mouse coordinates provided by the MouseEventArgs. void treeView1MouseUp(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { // Select the clicked node treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y); if(treeView1.SelectedNode != null) { myContextMenuStrip.Show(treeView1, e.Location); } } }

How to change the foreground or background colour of a selected cell in tkinter treeview?

@BryanOkley shared that one cannot change the color of an individual cell in ttk.Treeview. So I explored using tk.Canvas() and tk.Canvas.create_text() to create the illusion of changing the color of a selected cell in a ttk.Treeview() widget. I was fortunate to come by j08lue/ttkcalendar.py which had the same objective and I adapted from it. My … Read more

SPARQL query to get all parent of a node

Your data can be represented in RDF as data.n3: @prefix : <http://example.org/> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . :Network rdfs:subClassOf :Main . :ATM rdfs:subClassOf :Network . :ARPANET rdfs:subClassOf :Network . :Software rdfs:subClassOf :Main . :Linux rdfs:subClassOf :Software . :Windows rdfs:subClassOf :Software . :XP rdfs:subClassOf :Windows . :Win7 rdfs:subClassOf :Windows . :Win8 rdfs:subClassOf :Windows . From here, … Read more

Saving content of a treeview to a file and load it later

You can use BinaryFormatter to Serialize/Deserialize Nodes public static void SaveTree(TreeView tree, string filename) { using (Stream file = File.Open(filename, FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(file, tree.Nodes.Cast<TreeNode>().ToList()); } } public static void LoadTree(TreeView tree, string filename) { using (Stream file = File.Open(filename, FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(file); … Read more

the level of a treeview in WPF?

Given the Question:- so when I click a node, how do I know which level it is? is there workaround? Here’s a possible workaround:- If you have a reference to a Control in the Visual Tree, possibly from a Click event then you can use that control to work out which level it is in … Read more

Get a list of all tree nodes (in all levels) in TreeView Controls

You can use two recursive extension methods. You can either call myTreeView.GetAllNodes() or myTreeNode.GetAllNodes(): public static List<TreeNode> GetAllNodes(this TreeView _self) { List<TreeNode> result = new List<TreeNode>(); foreach (TreeNode child in _self.Nodes) { result.AddRange(child.GetAllNodes()); } return result; } public static List<TreeNode> GetAllNodes(this TreeNode _self) { List<TreeNode> result = new List<TreeNode>(); result.Add(_self); foreach (TreeNode child in _self.Nodes) … Read more