WPF TreeView-How to refresh tree after adding/removing node?

user610801 picture user610801 · Feb 10, 2011 · Viewed 16.7k times · Source

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?

Answer

Arsen Mkrtchyan picture Arsen Mkrtchyan · Feb 10, 2011

Use ObservableCollection instead of IList if you want to notify the user interface that something in the collection has changed