WPF TreeView-How to refresh tree after adding/removing node?
17,270
Use ObservableCollection instead of IList if you want to notify the user interface that something in the collection has changed
Author by
user610801
Updated on June 04, 2022Comments
-
user610801 almost 2 years
I refer to this article:
WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections
and modify the tree structure like:
Root |__Group |_Entry |_Source
In Entry.cs:
public class Entry { public int Key { get; set; } public string Name { get; set; } public ObservableCollection<Source> Sources { get; set; } public Entry() { Sources = new ObservableCollection<Source>(); } public ObservableCollection<object> Items { get { ObservableCollection<object> childNodes = new ObservableCollection<object>(); foreach (var source in this.Sources) childNodes.Add(source); return childNodes; } } }
In Source.cs:
public class Source { public int Key { get; set; } public string Name { get; set; } }
In XAML file:
<UserControl.CommandBindings> <CommandBinding Command="New" Executed="Add" /> </UserControl.CommandBindings> <TreeView x:Name="TreeView"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="TreeViewItem.IsExpanded" Value="True"/> </Style> </TreeView.ItemContainerStyle> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type local:Root}" ItemsSource="{Binding Items}"> <TextBlock Text="{Binding Path=Name}" IsEnabled="True"> </TextBlock> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}"> <TextBlock Text="{Binding Path=Name}" IsEnabled="True"> </TextBlock> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type local:Entry}" ItemsSource="{Binding Items}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Name}" IsEnabled="True"> <TextBlock.ContextMenu> <ContextMenu > <MenuItem Header="Add" Command="New"> </MenuItem> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </StackPanel> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type local:Source}" > <TextBlock Text="{Binding Path=Name}" /> </DataTemplate> </TreeView.Resources> </TreeView>
In UserControl.cs:
public ObservableCollection<Root> Roots = new ObservableCollection<Root>(); public UserControl6() { InitializeComponent(); //...Add new node manually TreeView.ItemsSource = Roots; } private void Add(object sender, ExecutedRoutedEventArgs e) { Entry ee = (Entry)TreeView.SelectedItem; Source s3 = new Source() { Key = 3, Name = "New Source" }; ee.Sources.Add(s3); }
When I click right button on specific node "Entry" to add a new node "Source" under Entry (call "Add" method), I add a new "Source" object under Entry successfully, but I can't see this new node on treeview. How to refresh treeview when adding/deleting node?
-
user610801 over 13 yearsOk! I change all IList and List to "ObservableCollection". Next, how do I notify user interface something in collection is changed? (Sorry, I'm new in WPF)
-
jcelgin about 9 years@user610801, just changing the collection type to ObservableCollection was enough for my WPF TreeView to update when I add/remove items from the colleciton.