Silverlight TreeView - 将项目添加到根节点不会更新 TreeView

发布于 2024-10-17 02:05:38 字数 1572 浏览 11 评论 0原文

我有一个自引用层次结构,它是一个 Ria 实体。我将实体放入 ObservableCollection 并将该集合绑定到我的 TreeView。一切都按预期工作,除了当我将实体添加到树的根级别时,UI 不会更新。

我在这个网站上发现了完全相同的问题。然而,这些问题的解决方案对我没有帮助。我不知道问题是WPF还是Silverlight的区别还是什么。

相关问题:
WPF TreeView 问题 1
WPF TreeView 问题 2

我的 XAML:

<sdk:TreeView.ItemTemplate>
    <sdk:HierarchicalDataTemplate ItemsSource="{Binding ChildTeams}">
        <TextBlock Text="{Binding Name, Mode=OneWay}"/>
    </sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>

加载实体:

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;
};

添加实体以编程方式创建新实体后到上下文:

context.Teams.Add(team);
context.SubmitChanges();

我的问题的核心是为什么将实体添加到较低级别的节点可以完美地工作,而添加到根却不能?

我可以手动将团队添加到集合中 (Entities.Add(team)),这将更新 UI,但不会更新数据库。我试图避免这种逻辑,以防止集合和实体集不同步。

我通过直接绑定到实体集来开始这一切,但这与 ObservableCollection 具有相同的行为:

treeView.ItemsSource = loadOperation.Entities.Where(t => t.ParentID == null);

任何帮助都是值得赞赏的。我已经从很多方面解决了这个问题,但无法让它顺利工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笏见 2024-10-24 02:05:38
<TextBlock Text="{Binding Name, Mode=OneWay}"/>

您是否尝试过使用 TwoWay 绑定?

由于您正在尝试将数据从 UI 添加回数据库,因此您需要有一个 TwoWay 绑定,或者也许我没有正确理解您的问题。

<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.

深海少女心 2024-10-24 02:05:38

现在我很清楚,我不应该期望:

  1. 将实体添加到上下文中以反映在 UI 中。我的实体 ObservableCollection 已与上下文 EntitySet 断开连接。
  2. 将实体添加到 OberservableCollection 以保存在数据库中。同样,它们是断开连接的,因此添加到 OberservableCollection 的实体不会神奇地最终出现在上下文中。
  3. 直接针对 UI 使用上下文中的 EntitySet,因为它没有实现 INotifyCollectionChanged。

SO 和其他地方有很多关于这一切的帖子。这篇文章总结得很好,并提到 EF 4.1 如何解决这个问题: ObservableCollection better than ObjectSet

As对于我最初的问题:为什么添加非根实体确实会更新 UI,我的猜测是父节点正在设法触发一个事件,因为它在子节点的 EntityCollection 中获取了一个新的子节点(可能通过 ListChanged)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文