Bindingsource不会暂停绑定 [英] Bindingsource is not suspending binding

查看:64
本文介绍了Bindingsource不会暂停绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示自定义详细信息的表单,一个部分是与客户相关联的银行帐户列表。此列表绑定到它自己的绑定源,因此在加载客户时,我会这样做:

I have a form that displays custom details, one section being a list of bank accounts associated with the customer. This list is bound to it's own bindingsource, so when loading a customer I do:

bsCustomer.DataSource = customer;
bsCustomerAccounts.DataSource = customer.Accounts;

我有一个 ObjectListView 绑定到 bsCustomerAccounts 。到目前为止,一切正常。

I have an ObjectListView that is bound to bsCustomerAccounts. So far everything works fine.

要编辑特定帐户,我双击它并打开一个单独的表单:

To edit a particular account, I double-click on it and open a separate form:

using (var form = new CustomerAccountForm(selectedAccount))
{
    DialogResult result = form.ShowDialog(this);
    if (result == DialogResult.OK)
    {
        selectedAccount= form.Account;
    }
}

问题是用户单击取消取消此表单中帐户的编辑。原始的 bcCustomerAccounts ,因此该列表仍在更新中。

The problem is when the user clicks on Cancel to cancel the editing of the account in this form. The original bcCustomerAccounts and therefore the list are still being updated.

我尝试了 SuspendBinding RaiseListChangedEvents = false 但是绑定源仍在更新。

I've tried SuspendBinding and RaiseListChangedEvents = false but the bindingsource is still being updated.

我遗漏了什么吗?

推荐答案

一开始似乎真的很令人惊讶,您认为当您没有将编辑后的对象分配回列表时,为什么要编辑列表项?

It seems really surprising at first, you think while you didn't assign the edited object back to the list, why is the list item edited?

键问题出在这里:类是引用类型

The key point is here: Classes are Reference Type.

您将所选帐户传递给了编辑表单,并且由于它是一个类,并且类是引用类型,因此实际上您正在编辑列表中的同一实例。因此,在编辑属性时,无论单击确定还是取消,所有编辑都将直接应用于对象。

You passed selected account to the edit form and since it's a class and classes are reference type, in fact you are editing the same instance which is in the list. So when you edit properties, all edits are directly applying to the object regardless of clicking OK or Cancel.

这篇关于Bindingsource不会暂停绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆