HierarchicalDataTemplate for TreeView programmatically (C#).I have this class:<br/> <pre lang="x-c#"> public class Folder { private string _name; internal string Name {get { return _name; }} private List&lt;Folder&gt; _subfolders; internal List&lt;Folder&gt; Subfolders { get { return _subfolders; } } public Folder() { _name = &quot;Root&quot;; _subfolders = new List&lt;Folder&gt;(); _subfolders.Add(new Folder(&quot;F1&quot;)); _subfolders[0].Subfolders.Add(new Folder(&quot;F1.1&quot;)); _subfolders[0].Subfolders.Add(new Folder(&quot;F1.2&quot;)); _subfolders.Add(new Folder(&quot;F2&quot;)); _subfolders[1].Subfolders.Add(new Folder(&quot;F2.1&quot;)); _subfolders[1].Subfolders.Add(new Folder(&quot;F2.2&quot;)); } public Folder(string name) { _name = name; _subfolders = new List&lt;Folder&gt;(); } }</pre> and I want display instance of this class in TreeView, but after this call TreeView is empty. Does anybode know what is wrong?<br/> <pre lang="x-c#"> private void Sample() { Folder folder = new Folder(); FrameworkElementFactory labelFactory = new FrameworkElementFactory(typeof(TextBlock)); //1. We have to define type of items HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(Folder)); //2. We have to define source for subitems template.ItemsSource = new Binding(&quot;Subfolders&quot;); //3. We have to define DataTemplate for items labelFactory.SetBinding(TextBlock.TextProperty, new Binding(&quot;Name&quot;)); template.VisualTree = labelFactory; treeView1.ItemTemplate = template; treeView1.ItemsSource = folder.Subfolders; }</pre> Regards© 2009 Microsoft Corporation. All rights reserved.Wed, 15 Jul 2009 22:47:00 Zacc87765-618e-4afd-b695-df5144d904achttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#acc87765-618e-4afd-b695-df5144d904achttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#acc87765-618e-4afd-b695-df5144d904ackicajhttp://social.msdn.microsoft.com/Profile/en-US/?user=kicajHierarchicalDataTemplate for TreeView programmatically (C#).I have this class:<br/> <pre lang="x-c#"> public class Folder { private string _name; internal string Name {get { return _name; }} private List&lt;Folder&gt; _subfolders; internal List&lt;Folder&gt; Subfolders { get { return _subfolders; } } public Folder() { _name = &quot;Root&quot;; _subfolders = new List&lt;Folder&gt;(); _subfolders.Add(new Folder(&quot;F1&quot;)); _subfolders[0].Subfolders.Add(new Folder(&quot;F1.1&quot;)); _subfolders[0].Subfolders.Add(new Folder(&quot;F1.2&quot;)); _subfolders.Add(new Folder(&quot;F2&quot;)); _subfolders[1].Subfolders.Add(new Folder(&quot;F2.1&quot;)); _subfolders[1].Subfolders.Add(new Folder(&quot;F2.2&quot;)); } public Folder(string name) { _name = name; _subfolders = new List&lt;Folder&gt;(); } }</pre> and I want display instance of this class in TreeView, but after this call TreeView is empty. Does anybode know what is wrong?<br/> <pre lang="x-c#"> private void Sample() { Folder folder = new Folder(); FrameworkElementFactory labelFactory = new FrameworkElementFactory(typeof(TextBlock)); //1. We have to define type of items HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(Folder)); //2. We have to define source for subitems template.ItemsSource = new Binding(&quot;Subfolders&quot;); //3. We have to define DataTemplate for items labelFactory.SetBinding(TextBlock.TextProperty, new Binding(&quot;Name&quot;)); template.VisualTree = labelFactory; treeView1.ItemTemplate = template; treeView1.ItemsSource = folder.Subfolders; }</pre> RegardsThu, 02 Jul 2009 18:52:00 Z2009-07-02T18:52:00Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#f1bc45bc-f523-4447-a73c-46f52c714fb0http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#f1bc45bc-f523-4447-a73c-46f52c714fb0Mark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyHierarchicalDataTemplate for TreeView programmatically (C#).Your code works for me....are you sure your Sample() method is getting called?<br/><br/>Where is treeView1 defined?<br/><br/><br/>Mark<br/><hr class="sig"> Mark Salsbery Microsoft MVP - Visual C++ Thu, 02 Jul 2009 23:45:02 Z2009-07-02T23:45:02Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#c471127e-e411-4c1c-9399-6fedda934fc6http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#c471127e-e411-4c1c-9399-6fedda934fc6kicajhttp://social.msdn.microsoft.com/Profile/en-US/?user=kicajHierarchicalDataTemplate for TreeView programmatically (C#).<p>Yes I am sure that Sample() is called :-)<br/>treeView1 is defined in XAML in Grid and Grid is defined in Window.</p>Fri, 03 Jul 2009 07:15:13 Z2009-07-03T07:15:13Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#36895e59-9720-445d-99fd-f08eb0d6f6adhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#36895e59-9720-445d-99fd-f08eb0d6f6adMark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyHierarchicalDataTemplate for TreeView programmatically (C#).<blockquote> <p>Yes I am sure that Sample() is called :-)<br/>treeView1 is defined in XAML in Grid and Grid is defined in Window.</p> </blockquote> <br/>I just realized I DID make a change to your code.....I did it almost subconsciously when I first copied it because I knew it wouldn't work correctly if I didn't change it...<br/><br/>You need to change any properties you bind to in your data class(es) to &quot;public&quot; instead of &quot;internal&quot;.<br/><br/><br/>Mark<br/><hr class="sig"> Mark Salsbery Microsoft MVP - Visual C++ Fri, 03 Jul 2009 16:27:48 Z2009-07-03T16:27:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#c8bf7a40-a7d5-4ce1-81fa-2b0f5245f4e6http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#c8bf7a40-a7d5-4ce1-81fa-2b0f5245f4e6kicajhttp://social.msdn.microsoft.com/Profile/en-US/?user=kicajHierarchicalDataTemplate for TreeView programmatically (C#).Now it works !<br/> Thanks !Sat, 04 Jul 2009 11:21:05 Z2009-07-04T11:21:05Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#f74e00ba-13bc-4cda-a855-05dc5d53bce0http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#f74e00ba-13bc-4cda-a855-05dc5d53bce0NothingToDohttp://social.msdn.microsoft.com/Profile/en-US/?user=NothingToDoHierarchicalDataTemplate for TreeView programmatically (C#).Will you please explain me how can I create the xaml for this example and please if you have any other example <br/>it will be better.<br/><br/>I want to be able to create a complex TreeView with images and text, with different formats for different levels and at the same <br/>time I don't want to use the namespace in the xaml to define the DataType and the ItemsSource, I want to do the binding programmatically if it is possible.<br/><br/>Thanks in advance.Mon, 13 Jul 2009 05:32:49 Z2009-07-13T05:32:49Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#92c42732-91fb-43c7-a4f0-d1b2808ca304http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#92c42732-91fb-43c7-a4f0-d1b2808ca304NothingToDohttp://social.msdn.microsoft.com/Profile/en-US/?user=NothingToDoHierarchicalDataTemplate for TreeView programmatically (C#).Any answer?Wed, 15 Jul 2009 05:08:04 Z2009-07-15T05:08:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#7cf7b232-18a0-42b9-beb2-c58ca2176bc5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/acc87765-618e-4afd-b695-df5144d904ac#7cf7b232-18a0-42b9-beb2-c58ca2176bc5Mark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyHierarchicalDataTemplate for TreeView programmatically (C#).<blockquote>Will you please explain me how can I create the xaml for this example</blockquote> <br/>For this example I used<br/><br/> <pre lang=x-xml>&lt;TreeView Name=&quot;treeView1&quot; /&gt;</pre> <br/><br/><br/>You may get better forum results if you start a new thread.<br/><br/><br/>Mark<br/><hr class="sig"> Mark Salsbery Microsoft MVP - Visual C++ Wed, 15 Jul 2009 22:47:00 Z2009-07-15T22:47:00Z