.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
HierarchicalDataTemplate for TreeView programmatically (C#).
HierarchicalDataTemplate for TreeView programmatically (C#).
- I have this class:
and I want display instance of this class in TreeView, but after this call TreeView is empty. Does anybode know what is wrong?public class Folder { private string _name; internal string Name {get { return _name; }} private List<Folder> _subfolders; internal List<Folder> Subfolders { get { return _subfolders; } } public Folder() { _name = "Root"; _subfolders = new List<Folder>(); _subfolders.Add(new Folder("F1")); _subfolders[0].Subfolders.Add(new Folder("F1.1")); _subfolders[0].Subfolders.Add(new Folder("F1.2")); _subfolders.Add(new Folder("F2")); _subfolders[1].Subfolders.Add(new Folder("F2.1")); _subfolders[1].Subfolders.Add(new Folder("F2.2")); } public Folder(string name) { _name = name; _subfolders = new List<Folder>(); } }
Regardsprivate 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("Subfolders"); //3. We have to define DataTemplate for items labelFactory.SetBinding(TextBlock.TextProperty, new Binding("Name")); template.VisualTree = labelFactory; treeView1.ItemTemplate = template; treeView1.ItemsSource = folder.Subfolders; }
Answers
Yes I am sure that Sample() is called :-)
treeView1 is defined in XAML in Grid and Grid is defined in Window.
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...
You need to change any properties you bind to in your data class(es) to "public" instead of "internal".
Mark
Mark Salsbery Microsoft MVP - Visual C++- Marked As Answer byBruce.ZhouMSFT, ModeratorMonday, July 06, 2009 9:15 AM
- Now it works !
Thanks !- Marked As Answer bykicaj Saturday, July 04, 2009 4:59 PM
All Replies
- Your code works for me....are you sure your Sample() method is getting called?
Where is treeView1 defined?
Mark
Mark Salsbery Microsoft MVP - Visual C++ Yes I am sure that Sample() is called :-)
treeView1 is defined in XAML in Grid and Grid is defined in Window.Yes I am sure that Sample() is called :-)
treeView1 is defined in XAML in Grid and Grid is defined in Window.
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...
You need to change any properties you bind to in your data class(es) to "public" instead of "internal".
Mark
Mark Salsbery Microsoft MVP - Visual C++- Marked As Answer byBruce.ZhouMSFT, ModeratorMonday, July 06, 2009 9:15 AM
- Now it works !
Thanks !- Marked As Answer bykicaj Saturday, July 04, 2009 4:59 PM
- Will you please explain me how can I create the xaml for this example and please if you have any other example
it will be better.
I want to be able to create a complex TreeView with images and text, with different formats for different levels and at the same
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.
Thanks in advance. - Any answer?
Will you please explain me how can I create the xaml for this example
For this example I used
<TreeView Name="treeView1" />
You may get better forum results if you start a new thread.
Mark
Mark Salsbery Microsoft MVP - Visual C++


