This is a MSDN question asked by WPF tree view how do I add to the selected item, the source is WPF tree view how do I add to the selected item.
public List<TreeViewList> treelist = new List<TreeViewList>();
private void AddFolder_Click(object sender, RoutedEventArgs e)
{
if (list.SelectedItem != null)
{
try
{
var listof = (list.ItemsSource as List<TreeViewList>);
int curIndex = listof.IndexOf(list.SelectedItem as TreeViewList);
listof[curIndex].Children.Add(person);
}
catch
{
var listof = (list.ItemsSource as List<TreeViewList>);
for (int a = 0; a < listof.Count; a++)
{
for (int b = 0; b < listof[a].Children.Count; b++)
{
if (list.SelectedItem == listof[a].Children[b])
{
listof[a].Children[b].Children.Add(person);
}
}
}
}
}
}
public class TreeViewList
{
public string Name { get; set; }
public TreeViewList()
{
Children = new ObservableCollection<TreeViewList>();
}
public ObservableCollection<TreeViewList> Children { get; set; }
}