views:

33

answers:

1

I have a TreeView with a few objects bound to it, let's say something like this:

public class House
    {
        public List<Room> Rooms { get; set; }
        public List<Person> People { get; set; }

        public House()
        {
            this.Rooms = new List<Room>();
            this.People = new List<Person>();
        }

        public void BuildRoom(string name)
        {
            this.Rooms.Add(new Room() { Name = name });
        }

        public void DestroyRoom(string name)
        {
            this.Rooms.Remove(new Room() { Name = name });
        }

        public void PersonEnter(string name)
        {
            this.People.Add(new Person() { Name = name });
        }

        public void PersonLeave(string name)
        {
            this.People.Remove(new Person() { Name = name });
        }
    }

    public class Room
    {
        public string Name { get; set; }
    }

    public class Person
    {
        public string Name { get; set; }
    }

The TreeView is watching over the House object, whenever a room is built / destroyed or a person enters / leaves, my tree view updates itself to show the new state of the house (I omitted some implementation details for simplicity).

What I want is to know the exact moment when this update finishes, so I can do something right there, the thing is that I created an indicator of the selected item, and when something moves, I need to update said indicator's position, that's the reason I need it exactly when the tree view updates.

Let me know if you know a solution to this.

Also, the code is not perfect (DestroyRoom and PersonLeave), but you get the idea.

Thanks!

A: 

Use an ObservableCollection<T> and wrap it in a CollectionView to get a bindable CurrentItem.

http://msdn.microsoft.com/en-us/library/system.windows.data.collectionview_members.aspx

Danny Varod
Actually this is what I have an ObservableCollection instead of lists, I shouldn't have avoided that detail. Anyway, when the CollectionChanged event happens, the treeview has still not updated its items, so that didn't work either.
Carlo
Are you using a hierarchical data template? - If not you will not get sub items. Is the tree view's itemsSource property binded correctly to the observable collection? (Try binding a label's content to the collection's count and another to the collection to test this - should get a number in one and the class name in the other).
Danny Varod