1

Have browsed similar answers, but I just can't find the problem.

I'm using MvvmLight, and have bound a WPF TreeView to an ObservableCollection in the ViewModel. The tree shows fine, but if I add members to the ObservableCollection later on, the tree doesn't refresh.

Here's the code:

// the data type that represents a tree node
public class FurnitureTreeNode : GalaSoft.MvvmLight.ViewModelBase
{
    public string Name { get; private set; }
    public object Data { get; private set; }
    public ObservableCollection<FurnitureTreeNode> ChildNodes { get; set;}
    public Furniture(string Name, ObservableCollection<FurnitureTreeNode> ChildNodes, object Data)
    {
        this.Name = Name;
        this.ChildNodes = ChildNodes;
        this.Data = Data;
    }
}

public class FurnituresViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    public ObservableCollection<FurnitureTreeNode> TopFurnitureNodes { get; set; }
    public ObservableCollection<FurnitureTreeNode> ChairNodes { get; set; }

    public FurnituresViewModel()
    {
        // initialize the top level furnitures collection
        TopFurnitureNodes = new ObservableCollection<FurnitureTreeNode>();

        // add two children
        TopFurnitureNodes.Add(new FurnitureTreeNode("Tables", null, null);
        TopFurnitureNodes.Add(new FurnitureTreeNode("Chairs", null, null);

        // add Beds only when they're ready
        BedsManager.ImportsSatisfied += AddBedsNode;
    }

The following runs fine, the collection is updated with the Beds node, but it isn't reflected in the TreeView:

    void AddBedsNode( object sender, EventArgs e )
    {
        Task.Run(() =>
        {
            TopFurnitureNodes.Add(new FurnitureTreeNode("Beds", null, null);
        }
        );
    }
}

Here's the XAML:

<TreeView Name="FurnituresTreeView" ItemsSource="{Binding TopFurnitureNodes}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}" DataType="x:Type FurnitureTreeNode">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Thank you!

Andrew
  • 817
  • 2
  • 10
  • 21

2 Answers2

1

Andrew,

Please try to replace

    Task.Run(() =>
    {
        TopFurnitureNodes.Add(new FurnitureTreeNode("Beds", null, null);
    }
    );

with:

TopFurnitureNodes.Add(new FurnitureTreeNode("Beds", null, null);

And let us know it this helps.

Please also make sure that you bind ViewModel to View correctly.

Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
0

Try implementing the INotifyPropertyChanged interface in your view model and raising the PropertyChanged event with the PropertyName parameter value of TopFurnitureNodes when you update it:

Task.Run(() =>
{
    TopFurnitureNodes.Add(new FurnitureTreeNode("Beds", null, null);
    RaisePropertyChanged("TopFurnitureNodes");
});

You shouldn't really need to do this, but it should work.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 2
    INotifyPropertyChanged is already implemented through the base class GalaSoft.MvvmLight.ViewModelBase. Isn't it RaisePropertyChanged("TopFurnitureNodes") ? Tried that, doesn't help. – Andrew Sep 12 '13 at 09:49
  • It might be... I don't use MvvmLight, so I don't know, but I think you understood what I meant. I'll update it in my answer all the same. – Sheridan Sep 12 '13 at 09:51