具有 MVVM 和 WCF Ria 服务的 Silverlights Dataform

发布于 2024-10-21 16:34:25 字数 1147 浏览 1 评论 0原文

晚上好,

我正在寻找有人帮助我了解如何使用 WCF Ria 服务将 Silverlight DataForm 与我的 ViewModel 结合起来。我想要完成的是使用 DataForm 管理我的实体集合并利用其内置的导航、添加、编辑和删除功能。然而,我在将它们与我的 ViewModel 和 Ria 服务结合在一起时遇到了麻烦。

根据我的理解,DataForm 需要绑定到 ObservableCollection。但是,当我从 WCF Ria 服务上下文查询时。 IE。

_context.Load(_context.GetAllCustomersQuery(), loadCustomersQueryCallback, true);

我将在回调方法中收到一个 IQueryable,我必须像这样将其转换为 ObservableCollection 吗?

Customers = new ObservableCollection(_context.Customers);

Customers 是我的 ViewModel 中的一个属性,如下所示...

    public ObservableCollection<Customer> Customers
    {
        get { return _customers; }
        set
        {
            if (_customers != value)
            {
                _customers = value;
                OnPropertyChanged("Customers");
            }
        }
    }

DataForm 绑定到 ViewModel 的 Customers 属性,我能够看到数据源中的数据,我可以在实体之间导航,我可以编辑现有实体并将更改保留回数据库,但是我无法添加或删除实体。

我对客户财产所做的事情正确吗?我是否通过转换为新的 ObservableCollection 来“断开”与上下文的连接,因此实际上并未从上下文中添加或删除实体?

非常感谢任何帮助。

问候,

Good evening,

I am looking for someone to help me with my understanding of how to incorporate the Silverlight DataForm with my ViewModel that using WCF Ria Services. What I am trying to accomplish is using the DataForm manage my collection of entity and utilize its built in navigation, Add, Edit and Delete functionality. Howerver, I am having trouble tying it all together with my ViewModel and Ria Services.

From my understanding, the DataForm needs to be bound to an ObservableCollection<T>. However when I query from the WCF Ria Service context. ie.

_context.Load(_context.GetAllCustomersQuery(), loadCustomersQueryCallback, true);

I will receive back an IQueryable in the callback method, which i would have to cast as an ObservableCollection<T> like so?

Customers = new ObservableCollection<Customer>(_context.Customers);

Customers is a property in my ViewModel like so...

    public ObservableCollection<Customer> Customers
    {
        get { return _customers; }
        set
        {
            if (_customers != value)
            {
                _customers = value;
                OnPropertyChanged("Customers");
            }
        }
    }

The DataForm is bound to the Customers Property of the ViewModel and I am able to see the data from my datasource, I can navigate between entities, I can edit an existing entity and persist the changes back to the database however I cannot add or delete entites.

Is what I'm doing with the Customers property correct? have I "disconnected" from the context by casting into a new ObservableCollection and therefore have not actually added or removed the entities from the context?

Any help is greatly appreciated.

Regards,

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

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

发布评论

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

评论(2

爱要勇敢去追 2024-10-28 16:34:26

你可以使用:

Customers.Clear(); // Or dispose every customer if it is Disposable
Customers = new ObservableCollection(result.ToList());

You could use :

Customers.Clear(); // Or dispose every customer if it is Disposable
Customers = new ObservableCollection(result.ToList());
月光色 2024-10-28 16:34:25

我认为你在最后一个问题中谈到了这个问题。当您创建新的 ObservableCollection 时,您已与集合更改跟踪(添加和删除)断开连接。除了使用 OC,还有许多其他选项值得考虑。在您的情况下,看起来 EntitySet 或 EntityList 可能是最好的选择。有关这些类型的完整概述,请查看我在 RIA 服务 SP1 中的集合绑定

I think you hit on the issue in your last question. When you create a new ObservableCollection, you've disconnected from collection change tracking (adds and deletes). Instead of using OC, there are a number of other options worth considering. In your case, it looks like EntitySet or EntityList may be the best options. For a full rundown of these types, take a look at my post on collection binding in RIA Services SP1.

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