Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF TreeView-How to refresh tree after adding/removing node?
    primarykey
    data
    text
    <p>I refer to this article:</p> <p><a href="https://stackoverflow.com/questions/1912481/wpf-treeview-hierarchicaldatatemplate-binding-to-object-with-multiple-child-col">WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections</a></p> <p>and modify the tree structure like:</p> <pre><code>Root |__Group |_Entry |_Source </code></pre> <p>In Entry.cs:</p> <pre><code>public class Entry { public int Key { get; set; } public string Name { get; set; } public ObservableCollection&lt;Source&gt; Sources { get; set; } public Entry() { Sources = new ObservableCollection&lt;Source&gt;(); } public ObservableCollection&lt;object&gt; Items { get { ObservableCollection&lt;object&gt; childNodes = new ObservableCollection&lt;object&gt;(); foreach (var source in this.Sources) childNodes.Add(source); return childNodes; } } } </code></pre> <p>In Source.cs:</p> <pre><code>public class Source { public int Key { get; set; } public string Name { get; set; } } </code></pre> <p>In XAML file:</p> <pre><code>&lt;UserControl.CommandBindings&gt; &lt;CommandBinding Command="New" Executed="Add" /&gt; &lt;/UserControl.CommandBindings&gt; &lt;TreeView x:Name="TreeView"&gt; &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type TreeViewItem}"&gt; &lt;Setter Property="TreeViewItem.IsExpanded" Value="True"/&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:Root}" ItemsSource="{Binding Items}"&gt; &lt;TextBlock Text="{Binding Path=Name}" IsEnabled="True"&gt; &lt;/TextBlock&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}"&gt; &lt;TextBlock Text="{Binding Path=Name}" IsEnabled="True"&gt; &lt;/TextBlock&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:Entry}" ItemsSource="{Binding Items}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Name}" IsEnabled="True"&gt; &lt;TextBlock.ContextMenu&gt; &lt;ContextMenu &gt; &lt;MenuItem Header="Add" Command="New"&gt; &lt;/MenuItem&gt; &lt;/ContextMenu&gt; &lt;/TextBlock.ContextMenu&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:Source}" &gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;/TreeView&gt; </code></pre> <p>In UserControl.cs:</p> <pre><code>public ObservableCollection&lt;Root&gt; Roots = new ObservableCollection&lt;Root&gt;(); 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); } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload