How to add to a treeview directory at runtime

I have a TreeView in which I would like to allow the user to add and remove subitems. When learning the basic functions, I use button and textbox to add this subitem. When the user clicks on the button , it is necessary to create a new TreeViewItem and set TreeView with text from the textbox set as a Header subpoint as a subpoint of my parent.

This is my current code in the button_click event:

 //ADD T_ITEM TO PARENT TREEVIEW private void button1_Click(object sender, RoutedEventArgs e) { TreeViewItem item = new TreeViewItem(); item.Header = textBox1.Text; //Compiler does not recognize "Nodes" Parent.Nodes.Add(item); } 

In particular, the compiler has a problem with Nodes . The basic question that I used to help me makes a lot of sense, but just doesn't work for me. All the sources I looked at use the Nodes command at the same time without any problems. Do I need to include a link, or is my code completely disabled?

- This guide uses System.Windows.Forms; to use Nodes , but doesn't seem to help, because I'm using the Windows Presentation Foundation.

Please show me how to make my code work in the right direction.

Thanks.

0
source share
1 answer

I did some more research and found an equivalent method for adding a child TreeViewItems to the parent TreeViewItems in WPF.

This is the change I made to my code:

 //ADD T_ITEM TO PARENT TREEVIEW private void button1_Click(object sender, RoutedEventArgs e) { TreeViewItem item = new TreeViewItem(); item.Header = textBox1.Text; Parent.Items.Add(item); } 
+1
source

Source: https://habr.com/ru/post/1494082/


All Articles