开发者

Silverlight TreeView - Adding item to root node does not update TreeView

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,开发者_如何学C 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.


<TextBlock Text="{Binding Name, Mode=OneWay}"/>

Did you try using TwoWay binding?

Since you are trying to add data form the UI back to the Database, you need to have a TwoWay binding inplace, or maybe I did not get your question correclty.


It's clear to me now that I should not have expected:

  1. Adding an entity to the context to reflect in the UI. My ObservableCollection of entities is disconnected from the context EntitySet.
  2. Adding an entity to the OberservableCollection to save in the DB. Again, they are disconnected, so an entity being added to the OberservableCollection won't magically end up in the context.
  3. To use the EntitySet from the context directly against the UI, since it doesn't implement INotifyCollectionChanged.

There are lots of posts on SO and elsewhere about all of this. This post sums it up well, and mentions how EF 4.1 addresses this: ObservableCollection better than ObjectSet

As to my original question of why adding non-root entities DID update the UI, my guess is the parent node was managing to fire off an event since it was getting a new child in its EntityCollection of child nodes (perhaps through ListChanged).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜