绑定自跟踪实体的导航属性 WPF 时出现问题

发布于 2024-09-25 23:53:23 字数 853 浏览 3 评论 0原文

我有一个使用实体框架 4 和自我跟踪实体消耗数据的 WPF 应用程序。其中我有一个包含 2 个控件的窗口,其中一个使用 ContentControl 和合并资源字典中的模板显示对象的“详细信息”部分。另一个是有问题的对象所属的组列表框和它可能所属的可用组的组合框,其中通过命令连接到控件的按钮,以根据组合框的 SelectedItem 从绑定的组集合中添加/删除项目。所有这些都通过 DependencyPropertys 绑定在一起。

在我的窗口中,我有用于对象的 DP,我们正在编辑的 EditedItem 和一个只读属性,其中包含它可能属于的组的列表,并通过 XAML 将其绑定到我的控件。

所以......

如果我创建我的实体之一的新实例,请像这样设置它的属性:(实际上这是确切的代码)

        Employee employee = Context.CreateObject<Employee>();
        employee.Name = "Joe Nobody's Brother Steve";
        employee.Active = true;
        employee.Username = "snobody";

        Group group = Context.CreateObject<Group>();
        group.Name = "Losers";
        group.DisplayName = "Spirit Squad";

        employee.Groups.Add(group);

并将其设置为我的窗口的 EditedItem 它工作完美!

但是,如果我从数据库中获取完全相同的实体,则组列表框为空。 有什么想法吗?

I have a WPF application consuming data using Entity Framework 4 and Self-Tracking Entities. In which I have a window with 2 controls in it, one that shows the "Details" portion of an object using a ContentControl and templates from a merged resource dictionary. Another with a ListBox of Groups the object in question belongs to and a ComboBox of available groups it could belong towith a button wired up via a command to the control to add/remove items from the bound collection of Groups based on the SelectedItem of the ComboBox. All this is bound together by DependencyPropertys.

In my Window I have DP's for the Object, EditedItem we are editing and a read only property with a List of Group of the groups it could belong to and bind that to my controls via XAML.

SO....

If I create a new instance of one of my entities, set it's properties like so: (Really this is the exact code)

        Employee employee = Context.CreateObject<Employee>();
        employee.Name = "Joe Nobody's Brother Steve";
        employee.Active = true;
        employee.Username = "snobody";

        Group group = Context.CreateObject<Group>();
        group.Name = "Losers";
        group.DisplayName = "Spirit Squad";

        employee.Groups.Add(group);

And set it as my Window's EditedItem it works FLAWLESSLY!

If I however fetch this exact same entity from my Database the Groups ListBox is empty.
Any ideas?

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

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

发布评论

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

评论(1

古镇旧梦 2024-10-02 23:53:23

事实证明我在其他地方犯了一个错误:
我需要在我的导航属性上调用:

    ObjectContext.LoadProperty(entity, navigationProperty);

以便填充它们。我认为这与我的对象全部派生自核心对象以及我在核心对象的 ObjectSet 上使用 OfType 选择它们这一事实有关。或者这可能是一种行为,但我想我以前就遇到过这种情况。

但是,嘿,我会开始工作,这很容易集成到我的选择方法和属性中。

将此归咎于对 EF4 的无知。

It turns out I had made a mistake else where:
I needed to call:

    ObjectContext.LoadProperty(entity, navigationProperty);

on my navigation properties for them to get populated. I think this has something to do with my objects all being derived from a core object and the fact that I select them using OfType on the ObjectSet of the core object. Or it could be behavior but I would think I would have encountered it before now.

But hey I'll take working, and this is easy enough to integrate into my selection methods and properties.

Chalk this one up to ignorance of EF4.

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