Create a Treeview in WPF

Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK



Step 2: Drag and drop the TreeViewItem.



Code:

  1. <TreeViewItem Header="Developer" Tag="Group" >  
  2. <TreeViewItem Header="Romil" />  
  3. <TreeViewItem Header="Prabhjot" />  
  4. <TreeViewItem Header="Feroz" />  
  5. <TreeViewItem Header="Shyamal" />  
  6. <TreeViewItem Header="Tester" Tag="Group" >  
  7. <TreeViewItem Header="Suketu" />  
  8. <TreeViewItem Header="Monali" />  
  9. <TreeViewItem Header="Ayushi" />  
  10. <TreeViewItem Header="Mathan" />  
  11. </TreeViewItem>  
  12. </TreeViewItem>  
The complete page of MainWindow.xaml will be like:
  1. <Window x:Class="Treeview_wpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">  
  2.     <Grid>  
  3.         <TreeViewItem Header="Developer" Tag="Group">  
  4.             <TreeViewItem Header="Romil" />  
  5.             <TreeViewItem Header="Prabhjot" />  
  6.             <TreeViewItem Header="Feroz" />  
  7.             <TreeViewItem Header="Shyamal" />  
  8.             <TreeViewItem Header="Tester" Tag="Group">  
  9.                 <TreeViewItem Header="Suketu" />  
  10.                 <TreeViewItem Header="Monali" />  
  11.                 <TreeViewItem Header="Ayushi" />  
  12.                 <TreeViewItem Header="Mathan" />  
  13.             </TreeViewItem>  
  14.         </TreeViewItem>  
  15.   
  16.     </Grid>  
  17. </Window>  
Step 3: Now run the code.

 

Ebook Download
View all
Learn
View all