I have a List that I've bound to a TreeView. Setting TreeView.DataContext works - everything displays correctly. I then change the list (add an item to it) and set TreeView.DataContext again (to the same value) but the tree does not refresh with the new items. How do I get the treeview to refresh?
This is basically my code:
public class xItemCollection : ObservableCollection<xItem>
{
}
public class xItem : INotifyPropertyChanged
{
xItemCollection _Items;
string m_Text;
public xItem()
{
_Items = new xItemCollection();
}
public xItemCollection Items {get{return _Items;}}
public string Text {get{return m_Text;} set{m_Text=value;}}
}
class MyProgram
{
xItem m_RootItem;
void UpdateTree()
{
this.RootItem = new xItem();
treeView.DataContext = this;
}
public xItem RootItem
{
get { return m_RootItem;}
set { m_RootItem = value;}
}
}
The xaml is:
<TreeView Name="Tree" ItemsSource="{Binding Path=RootItem.Items}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Text}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Adding items to the list works until the tree is rendered for the first time. After it is rendered, adding/removing items does not refresh the tree.