I have a self-referencing hierarchy that is a Ria Entity. I place the entities into an ObservableCollection and bind the collection to my TreeView. Everything works as expected, except when I add an entity to the root level of the tree, the UI is not updated.
I have found the exact same question on this site. However, the solutions in those questions are not helping me. I don't know if the problem is the difference between WPF or Silverlight or what.
Related Questions:
WPF TreeView Question 1
WPF TreeView Question 2
My XAML:
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding ChildTeams}">
<TextBlock Text="{Binding Name, Mode=OneWay}"/>
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
Loading the entities:
var query = context.GetTeamsQuery();
var loadOperation = context.Load(query);
loadOperation.Completed += (sender, e) =>
{
Entities.Clear();
foreach(var item in loadOperation.Entities.Where(t => t.ParentID == null))
{
Entities.Add(item);
}
treeView.ItemsSource = Entities;
};
Adding the entity to the context once the new entity is programatically created:
context.Teams.Add(team);
context.SubmitChanges();
The core of my question is why does adding entities to lower level nodes work perfectly, while adding to the root does not?
I can manually add a team to the collection (Entities.Add(team)) and this will update the UI, but not the database. I'm trying to avoid such logic to prevent the collection and entity set from becoming out of sync.
I started all of this by just binding straight to the entity set, but that has the same behavior as the ObservableCollection:
treeView.ItemsSource = loadOperation.Entities.Where(t => t.ParentID == null);
Any help is appreciated. I've tackled this from many sides and can't get this to work as smoothly as it should.