Hello.
Wrote code for adding and deleting selected branches in the tree. But there is a strange and wrong behavior. For example, if I add one child element 1 I select it and click on the button a child is added to it, if I select 2 a child is added to it, and I need to add only to the element that is located next to the button, and not to the others. It may be possible to do a check through the Tag property, but the Tag property is constantly changing. How to fix the problem with the buttons?
TreeViewItem sel = new TreeViewItem(); public void treeviewS_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { sel = treeviewS.SelectedItem as TreeViewItem; } if (sel.IsSelected == true) { int sum = 1; for (x = 0; x <= sel.Items.Count;) { sum *= x; x++; newChild2 = new TreeViewItem(); newChild2.Header = "Подраздел " + sel.Tag.ToString().Replace("s", string.Empty) + "-" + x.ToString(); newChild2.Tag = "s".TrimStart().ToString() + sel.Tag.ToString() + "-" + x.ToString(); TreeViewItem childparentSystemCatalog2 = new TreeViewItem(); sp = new StackPanel(); sp.Orientation = System.Windows.Controls.Orientation.Horizontal; tb2 = new TextBlock(); tb2.Text = "Подразделы " + newChild2.Tag.ToString(); tb2.Tag = newChild2.Tag.ToString(); bb5 = new System.Windows.Controls.Button(); bb5.Margin = new Thickness(10, 0, 0, 0); bb5.ToolTip = "Добавить подраздел"; bb5.Background = System.Windows.Media.Brushes.LightGreen; bb5.Content = "доб."; bb5.Click += new RoutedEventHandler(bb5_Click); System.Windows.Controls.Button b5 = new System.Windows.Controls.Button(); b5.Margin = new Thickness(10, 0, 0, 0); b5.ToolTip = "Удалить подраздел"; b5.Background = System.Windows.Media.Brushes.LightGreen; b5.Content = "удал."; b5.Click += new RoutedEventHandler(b5_Click); sp.Children.Add(tb2); sp.Children.Add(bb5); sp.Children.Add(b5); childparentSystemCatalog2.Header = sp; childparentSystemCatalog2.Tag = " " + newChild2.Tag.ToString(); newChild2.Items.Add(childparentSystemCatalog2); } sel.Items.Add(newChild2); }