Working TreeView Control in C#

Image1.jpg

Copy the contents of one treeview to another treview.

XAML CODE :

<TreeViewItem Header="Andra&lt;Pradesh" >

<TreeViewItem Header="Hyderabad" />
 
<TreeViewItem Header="Vizag" />
<
TreeViewItem Header="Thirupathi" />
</TreeViewItem>
<
TreeViewItem Header="Kerala" >
<
TreeViewItem Header="Kazikode" />
<
TreeViewItem Header="Thiruvananthapuram" />
</
TreeViewItem>
<
TreeViewItem Header="Maharashtra" >
    
<TreeViewItem Header="Mumbai" />
    
<TreeViewItem Header="Pune" />
    
<TreeViewItem Header="Goa" />
</
TreeViewItem> 

CS CODE :

To copy selected child

private void button1_Click(object sender, RoutedEventArgs e)

{
     TreeViewItem tv = (treeView1.SelectedItem) as TreeViewItem;

     string co = tv.Header.ToString();

     treeView2.Items.Add(co);

}
          

To delete al the childs in tree

private void button4_Click(object sender, RoutedEventArgs e)

{           

     treeView2.Items.Clear();          

}

To delete selected child

private void button2_Click(object sender, RoutedEventArgs e)

{           

     treeView2.Items.Remove(treeView2.SelectedItem);         

}
Next Recommended Reading Treeview control with XML in ASP.NET