Populate TreeView from DataBase

It will probably be something like this. Give some more detail as to what exactly you want to do if you need more. //In Page load foreach (DataRow row in topics.Rows) { TreeNode node = new TreeNode(dr[“name”], dr[“topicId”]) node.PopulateOnDemand = true; TreeView1.Nodes.Add(node); } /// protected void PopulateNode(Object sender, TreeNodeEventArgs e) { string topicId = e.Node.Value; … Read more

Maintain scroll position of treeview

I’m not a VB guy but in C# I do it this way: Some Win32 native functions: [DllImport(“user32.dll”, CharSet = CharSet.Unicode)] public static extern int GetScrollPos(IntPtr hWnd, int nBar); [DllImport(“user32.dll”, CharSet = CharSet.Unicode)] public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw); private const int SB_HORZ = 0x0; private const int SB_VERT … Read more

Having HierarchicalDataTemplates in a TreeView

You dont need a nested template here, since TreeView control will take care of nesting it based on the DataType it requires. So just define Two HierarchicalDataTemplates for Album and Artist Type and one ordinary DataTemplate for your Track class. <HierarchicalDataTemplate DataType=”{x:Type local:Artist}” ItemsSource=”{Binding Albums}” > <TextBlock Text=”{Binding Name}”/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType=”{x:Type local:Album}” ItemsSource=”{Binding Tracks}” … Read more

Binding hierarchical xml to treeview

A solution that doesn’t need to parse XML in order to bind its contents to a TreeView doesn’t exist (and if it exits, internally, of course, XML is parsed). Anyway you could implement this yourself by using LINQ to XML: private void Form1_Load(object sender, EventArgs e) { var doc = XDocument.Load(“data.xml”); var root = doc.Root; … Read more

Two-way binding of Xml data to the WPF TreeView

Well, it would be easier if your element hierarchy was more like… <node type=”forest”> <node type=”tree”> … …rather than your current schema. As-is, you’ll need 4 HierarchicalDataTemplates, one for each hierarchical element including the root, and one DataTemplate for leaf elements: <Window.Resources> <HierarchicalDataTemplate DataType=”forestPad” ItemsSource=”{Binding XPath=forest}”> <TextBlock Text=”a forestpad” /> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType=”forest” ItemsSource=”{Binding XPath=tree}”> … Read more