How to bind TreeView with nested object without using HierarchicalDatatemplate
-
Hello All, Is there any way to bind TreeView with nested object without using HierarchicalDatatemplate? For example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { public Window1() { InitializeComponent(); List _family = new List(); Person mom = new Person() { Name = "Hteik Tin May" }; mom.Children.Add(new Person() { Name = "Michael Sync" }); mom.Children.Add(new Person() { Name = "Shwesin Sync" }); mom.Children.Add(new Person() { Name = "Aye Nyein Thu" }); mom.Children.Add(new Person() { Name = "Kyaw Zin Thu" }); _family.Add(mom); familyTreeView.ItemsSource = _family; familyTreeView.DisplayMemberPath = "Name"; } } public class Person { readonly List _children = new List(); public IList Children { get { return _children; } } public string Name { get; set; } } } It's not working without HierarchicalDatatemplate. Any idea? Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Hello All, Is there any way to bind TreeView with nested object without using HierarchicalDatatemplate? For example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window { public Window1() { InitializeComponent(); List _family = new List(); Person mom = new Person() { Name = "Hteik Tin May" }; mom.Children.Add(new Person() { Name = "Michael Sync" }); mom.Children.Add(new Person() { Name = "Shwesin Sync" }); mom.Children.Add(new Person() { Name = "Aye Nyein Thu" }); mom.Children.Add(new Person() { Name = "Kyaw Zin Thu" }); _family.Add(mom); familyTreeView.ItemsSource = _family; familyTreeView.DisplayMemberPath = "Name"; } } public class Person { readonly List _children = new List(); public IList Children { get { return _children; } } public string Name { get; set; } } } It's not working without HierarchicalDatatemplate. Any idea? Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
Any specific reason you don't want to use this HierarchicalDataTemplate? You can always add TreeViewItems manually.
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
-
Any specific reason you don't want to use this HierarchicalDataTemplate? You can always add TreeViewItems manually.
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.
I want to depend on the structure of my object. not Hierachical DataTemplate which is static. or Am I missing something? What should I do if I want to change the level of treeview? For example: 2 level Hierachical structure to 5 level Hierachical 2 level Hierachical structure -111 ---22222 ---22222 -333 ---44444 ---44444 5 level Hierachical structure -111 ---22222 --------333333333333 -------------44444444444 -------------------------55555 ---22222 -333 ---44444 ---44444
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
I want to depend on the structure of my object. not Hierachical DataTemplate which is static. or Am I missing something? What should I do if I want to change the level of treeview? For example: 2 level Hierachical structure to 5 level Hierachical 2 level Hierachical structure -111 ---22222 ---22222 -333 ---44444 ---44444 5 level Hierachical structure -111 ---22222 --------333333333333 -------------44444444444 -------------------------55555 ---22222 -333 ---44444 ---44444
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
Michael, Please check out my blg entry here. http://karlshifflett.wordpress.com/2008/02/03/wpf-sample-series-databound-hierarchicaldatatemplate-menu-sample/[^] It covers this scenario in detail.
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | XAML Power Toys Home PageJust a grain of sand on the worlds beaches.