Bindable TreeCollection RRS feed

  • Question

  • Hi,

    I need to create TreeCollection, which i can bind to WPF UI components like TreeView, Listbox etc. Can anyone suggest right approach and what i have to consider creating such object.




    Wednesday, March 7, 2007 2:24 PM

All replies

  • WPF is capable of binding to any collection that implements IEnumerable.  The base class ItemsControl has a property called ItemsSource which you set to the collection you are binding to.

    However it's advisable to base your collections on ObservableCollection (which implements INotifyCollectionChanged).  This will ensure that when items are added to or removed from the collection your ItemsControl is automatically updated.

    Wednesday, March 7, 2007 3:13 PM
  • Thanks for your reply,

    I am aware of INotifyCollectionChanged and INotifyPropertyChanged interfaces, thing is I do not know how to design or customize a collection (TreeCollection) to be bindable to TreeView and similar controls using DataTemplates (to make TreeView display my data hierarchicaly as they reside in TreeCollection) .

    I was unable to bind today avaiable TreeCollections. So I am afraid i will have to create my own :(



    Wednesday, March 7, 2007 3:31 PM
  • What is TreeCollection?

    Hard to advise but if TreeCollection has virtual OnAdd() and OnRemove ()methods, you could derive a class off it and implement INotifyCollectionChanged.. e.g

    public class ObservableTreeCollection : TreeCollection, INotifyCollectionChanged

    You can then override the OnAdd and OnRemove and fire the CollectionChanged event within there.

    Hope that helps
    Wednesday, March 7, 2007 3:47 PM
  • Hi Neil, thanks for your reply

    I found a collection (NGenerics collections) that may be useful. But still, do u know hot to bind such collection to TreeView? I found some how tos regarding hierarchical data binding, but i need a recursive way (because I do not know how many levels my tree will have). Here is what i do now: 

       52 NGenerics.DataStructures.GeneralTree<INavigatorTreeItem> t = new NGenerics.DataStructures.GeneralTree<INavigatorTreeItem>(new TreeItem("root", "Page2.xaml"));

       53             for (int i = 0; i < 10; i++)

       54             {

       55                 t.Add(new TreeItem(string.Format("item no. {0}", i), "Page1.xaml"));

       56                 for (int j = 0; j < 10; j++)

       57                 {

       58                     tIdea.Add(new TreeItem(string.Format("subitem no. {0}", j), "Page1.xaml"));

       59                     for (int k = 0; k < 10; k++)

       60                     {

       61                         tIdea[j].Add(new TreeItem(string.Format("subsubitem no. {0}", k), "Page1.xaml"));

       62                     }

       63                 }

       64             }

       65             pageGrid.DataContext = t;

    I make a GeneralTree collection with three levels. Each tree node is just another GeneralTree object, each node has a "Data" property. In this property are stored real data you passed to the tree.

    Now, I do not know how big this collection will be or how many levels it will contain, but i want to bind it to the TreeView. I tried something like this:  

       23 <TreeView x:Name="tree1" Grid.Column="0" SelectedItemChanged="SelectedItemChanged"  ItemsSource="{Binding}">

       24         <TreeView.ItemTemplate>

       25           <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}">

       26             <TextBlock Text="{Binding Path=Data.Name}"/>

       27             <HierarchicalDataTemplate.ItemTemplate>

       28               <DataTemplate>

       29                 <TextBlock Text="{Binding Path=Data.Name}"/>

       30               </DataTemplate>

       31             </HierarchicalDataTemplate.ItemTemplate>

       32           </HierarchicalDataTemplate>

       33         </TreeView.ItemTemplate>

       34       </TreeView>

    This shows items correctly, but only first two levels. Now is there any way to do the same thing recursive to show all levels in tree?

    Thanks for suggestions.

    Thursday, March 8, 2007 9:48 AM