Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to bind observable collection to Wpf Tree view? give me good example
Posted

By using ItemSource property you can bind treeview to Observable collection
For Eg:-
XML
<treeview itemssource="{x:Static local:Window1.AnimalCategories}">

   <treeview.itemtemplate>
     <hierarchicaldatatemplate itemssource="{Binding Path=Animals}">

       <textblock fontweight="Bold" text="{Binding Path=Category}" />

       <hierarchicaldatatemplate.itemtemplate>

         <datatemplate>

           <textblock text="{Binding Path=Name}" />

         </datatemplate>
       </hierarchicaldatatemplate.itemtemplate>

     </hierarchicaldatatemplate>

   </treeview.itemtemplate>

 </treeview>


for more information please check this link

WPF Treeview: Styling and Template Binding using MVVM[^]

Thanks
Sujeet
 
Share this answer
 
Hi. if you define your ObservableCollection in code behind, you must set x:name for your treeView and in its loaded event write:

private void myTreeView_Loaded(object sender,RoutedEventArgs e)
{
myTreeView.DataContext=this.DataContext;
}

and in your xaml set ItemsSource :
ItemsSource="{Binding}"

GoodLuck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900