Click here to Skip to main content
15,887,371 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a treeview to which I need to add new child nodes when another node is selected. I have a button and I need to allow the user to add a new child node to a selected node when that button is pressed. How do I get that done good folks? :)

I have a OnItemSelected method too:

C#
private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
           nameTxt.Text = e.Source.ToString();
}
Posted

1 solution

Please see:
https://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms589116(v=vs.110).aspx[^].

Now, let's follow these help pages. Create a WPF application from scratch and add some sample code to main window. This is a complete file:
C#
using System.Windows;
using System.Windows.Controls;

namespace SampleApplication.Ui {

    public partial class MainWindow : Window {

        public MainWindow() {
            // InitializeComponent(); // not really needed,
            // because this sample is fully programmatical;
            // you don't even need XAML if you properly write
            // your entry-point method (Main)
            TreeView treeView = new TreeView();
            TreeViewItem top = new TreeViewItem();
            top.Header = "sample header";
            TreeViewItem child = new TreeViewItem();
            child.Header = "sample child";
            TreeViewItem grandchild = new TreeViewItem();
            grandchild.Header = "sample grandchild";
            treeView.Items.Add(top);
            top.Items.Add(child);
            child.Items.Add(grandchild);
            this.AddChild(treeView);
        } //MainWindow

    } //class MainWindow

} //namespace SampleApplication.Ui

In practice, however, this is not very typical usage. In most cases, you need to learn data binding. See, for example, http://www.wpf-tutorial.com/treeview-control/treeview-data-binding-multiple-templates[^].

See also this more advanced CodeProject article: Simplifying the WPF TreeView by Using the ViewModel Pattern[^].

—SA
 
Share this answer
 
v3
Comments
Dinuka Jayasuriya 12-Mar-15 1:02am    
Great! :D How do you remove a node Sergey?
Sergey Alexandrovich Kryukov 12-Mar-15 1:12am    
Look at the referenced articles again. Apparently, in the type where I found "Add" there are Remove and RemoveAt (wasn't it pretty much obvious? :-). See the previous link.

Now, I already answered your original question. So, will you please accept it formally now?

—SA

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