Bindingsource 没有暂停绑定 [英] Bindingsource is not suspending binding

查看:16
本文介绍了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;
    }
}

问题是当用户点击Cancel取消对该表单的账户编辑时.原始的 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.

我已经尝试了 SuspendBindingRaiseListChangedEvents = 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?

关键点在这里:类是引用类型.

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

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天全站免登陆