WPF tree view how do I add to the selected item

Charles He-MSFT 96 Reputation points Microsoft Employee
2020-03-27T08:29:26.57+00:00

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; }      
        }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-27T08:56:58.087+00:00

    Hi,

    Welcome to our Q&A platform!

    The reply from Peter Fleischer

    you can simplified your code.

    1. use ObservableCollection instead of list,
    2. use SelectedItemChanged event to get SelectedItem.

    Try following demo. Demo use Interaction and MVVM.

    XAML:

    <Window x:Class="WpfApp1.Window13"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp13"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            mc:Ignorable="d"
            Title="Window13" Height="450" Width="800">
      <Window.DataContext>
        <local:ViewModel/>
      </Window.DataContext>
      <StackPanel>
        <Button Content="Add new person" Command="{Binding Cmd}"/>
        <TreeView ItemsSource="{Binding View}" Margin="5">
          <i:Interaction.Behaviors>
            <local:TreeViewBehavior/>
          </i:Interaction.Behaviors>
          <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:TreeViewList}"
                                      ItemsSource="{Binding Children}">
              <Label Content="{Binding Name}"/>
            </HierarchicalDataTemplate>
          </TreeView.Resources>
        </TreeView>
      </StackPanel>
    </Window>
    

    Code:

    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Interactivity;
    
    namespace WpfApp13
    {
      public class ViewModel
      {
        public ObservableCollection<TreeViewList> treelist = new ObservableCollection<TreeViewList>();
        public ObservableCollection<TreeViewList> View { get => treelist; }
    
        public TreeViewList SelectedItem { get; set; }
    
        public ICommand Cmd { get => new RelayCommand(AddFolder_Click); }
    
        private int nr = 1;
        private TreeViewList person { get => new TreeViewList() { Name = $"Name {nr++}" }; }
    
        private void AddFolder_Click(object state)
        {
          if (SelectedItem == null) treelist.Add(person); else SelectedItem.Children.Add(person);
        }
      }
    
      public class TreeViewList
      {
        public string Name { get; set; }
        public ObservableCollection<TreeViewList> Children { get; set; } = new ObservableCollection<TreeViewList>();
      }
    
      public class TreeViewBehavior : Behavior<TreeView>
      {
        protected override void OnAttached()
        {
          AssociatedObject.SelectedItemChanged += AssociatedObject_SelectedItemChanged;
        }
    
        private void AssociatedObject_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
          var tv = sender as TreeView;
          if (tv == null) return;
          var vm = tv.DataContext as ViewModel;
          if (vm == null) return;
          vm.SelectedItem = tv.SelectedItem as TreeViewList;
        }
      }
    
      public class RelayCommand : ICommand
      {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _action;
        public RelayCommand(Action<object> action) { _action = action; _canExecute = null; }
        public void Execute(object o) => _action(o);
        public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
        public event EventHandler CanExecuteChanged
        {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
        }
      }
    }
    

    Thanks.

    0 comments No comments