question

CharlesHe-MSFT avatar image
0 Votes"
CharlesHe-MSFT asked ·

WPF tree view how do I add to the selected item

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.