views:

123

answers:

2

I am using a Wpf TreeView, in this I can add TreeViewItems dynamically. Is there any way to know when the tree is updated? I tried with the CollectionChanged event of the ObservableCollection that I binded with the TreeView but that didn't work.

Edit:

My code is in like this:

class Temp
{
    public void Load()
    {
        DeriveA d1 = new DeriveA();
        DeriveB d2 = new DeriveB();
        DeriveB d3 = new DeriveB();
        DeriveC d4 = new DeriveC();
        DeriveC d5 = new DeriveC();
        d1.Items.Add(d2);
        d1.Items.Add(d3);
        d2.Items.Add(d4);
        d2.Items.Add(d5);

        List = new ObservableCollection<object>();
        List.Add(d1);
        tree.ItemsSource = List;

        DeriveC d6 = new DeriveC();
        d3.Items.Add(d6);        //At this point, I want to know that List got updated
    }

    public ObservableCollection<object> List
    {
        get;
        set;
    }
}

class Base
{
    ObservableCollection<Base> Items = new ObservableCollection<Base>();
}

class DeriveA : Base
{
}

class DeriveB : Base
{
}

class DeriveC : Base
{
}

How can I find when List property get updated at any level?

A: 

What about SourceUpdated event?


EDIT The SourceUpdated event doesn't fire, but I tried with a ObservableCollection.CollectionChanged and it is ok, so I would ask to give a little code of yours, here is what I have tested, hope it helps:

public partial class Window1 : Window
{
    ObservableCollection<string> items = new ObservableCollection<string>()
    {
        "string1","string2","string3","string4","string5"
    };
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
        tree.ItemsSource = items;
        items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(items_CollectionChanged);
    }

    void items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        MessageBox.Show("Event raised");
    }


    private void btnAddItem_Click(object sender, RoutedEventArgs e)
    {
        items.Add("string6");
    }
}

and the xaml

<StackPanel Orientation="Vertical">
            <TreeView x:Name="tree" />
            <Button x:Name="btnAddItem" Click="btnAddItem_Click" Content="AddItem" />
</StackPanel>
Svetlozar Angelov
It doesn't work, anyway thanks for reply! Is there something else?
viky
I have updated my code, plz check it!
viky
I don't see if you've subscribed for the CollectionChangedEvent, try List.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(List_CollectionChanged); and void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { MessageBox.Show("Event raised"); }
Svetlozar Angelov
A: 

you could have something like this:

private void Loaded()
{
    MyTreeNode d1 = new MyTreeNode();
    MyTreeNode d2 = new MyTreeNode();
    MyTreeNode d3 = new MyTreeNode();
    MyTreeNode d4 = new MyTreeNode();
    MyTreeNode d5 = new MyTreeNode();
    d1.AddNode(d2);
    d1.AddNode(d3);
    d2.AddNode(d4);
    d2.AddNode(d5);

    d1.CollectionChanged += (s, e) =>
    {
        MessageBox.Show("Tree Changed");
    };

    MyTreeNode d6 = new MyTreeNode();
    d3.AddNode(d6);
}  

public class MyTreeNode : ObservableCollection<MyTreeNode>
{
    public void AddNode(MyTreeNode node)
    {
        this.Add(node);
        node.CollectionChanged += (s, e) => this.OnCollectionChanged(e);
    }
}
redoced