如何防止CurrencyManager调用绑定对象的BeginEdit()/EndEdit()方法
我有一个带有几个文本框和一个数据网格的表单。一个业务实体可以绑定到此表单。例如,BO 看起来像这样:
class BO : IEditableObject, INotifyPropertyChanged
{
public string FirstName {get; set;}
public string LastName {get; set}
public BindingList<BO> Relatives {get; set}
// implementation of the interfaces
}
所以在表单上,FirstName & LastName 绑定到文本框,Relatives 绑定到网格。另外,在表单上我有按钮“保存”和“取消”。单击“保存”按钮时,我调用 IEditableObject.EndEdit(),单击“取消”按钮时,我调用 IEditableObject.CancelEdit()。我希望 CancelEdit() 方法拒绝用户所做的所有更改,包括绑定到网格的亲戚的更改。到目前为止一切顺利..
但是 网格控件使用CurrencyManager(网格实际上是Devexpress控件,但这并不重要,因为我相信WinForms控件也使用它)。并且CurrencyManager 调用BeginEdit() &每次用户更改行时,Relatives 集合中的项目都会使用 EndEdit() 。因此,当单击“Cancel()”按钮时,仅“名字”和“名字”发生变化。 LastName将被取消,因为对于Relatives集合中的子对象,EndEdit()已经被网格的底层CurrencyManger调用了!那么问题来了——如何防止CurrencyManager调用这些方法,以便我可以通过一次调用拒绝所有更改?
谢谢!
I've got a form with several textboxes and one datagrid. One business entity can be bound to this form. For example, BO looks like this:
class BO : IEditableObject, INotifyPropertyChanged
{
public string FirstName {get; set;}
public string LastName {get; set}
public BindingList<BO> Relatives {get; set}
// implementation of the interfaces
}
So on the form, FirstName & LastName are bound to the textboxes and Relatives is bound to the grid. Also on the form I have buttons Save and Cancel. On clicking Save button I call IEditableObject.EndEdit() and on clicking Cancel button I call IEditableObject.CancelEdit(). I want CancelEdit() method to reject all changes made by the user, including changes in Relatives which is bound to the grid. So far so good..
BUT
The grid control uses CurrencyManager (the grid is Devexpress control actually but it doesn't matter since I believe WinForms control uses it too). And CurrencyManager calls BeginEdit() & EndEdit() for items in Relatives collection every time the user changes the row. So when button Cancel() is clicked only changes in FirstName & LastName will be cancelled because for the child objects in Relatives collection EndEdit() was already called by the grid's underlying CurrencyManger! So, the question - how to prevent CurrencyManager from calling that methods so that I could reject all changes by one call?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您唯一的解决方案是从业务对象中删除
IEditableObject
的实现。当绑定对象发生更改时,CurrencyManager
(用于 Winforms 中的所有绑定)会自行执行此操作。您要么必须更改逻辑以处理更高级别的更改跟踪,要么从类中删除接口但保留方法。这样做意味着您每次都必须显式调用
BeginEdit
、EndEdit
和CancelEdit
。Your only solution would be to remove the implementation of
IEditableObject
from your business object. TheCurrencyManager
(which is used for all bindings in Winforms) does this on its own when the bound object changes.You'll either have to change your logic to handle higher-level change tracking or remove the interface from your class but leave the methods. Doing this will mean you'll have to call
BeginEdit
,EndEdit
, andCancelEdit
explicitly every time.如果您有 Bindingsource,则可以在 CurrentChanged 事件上调用 EndEdit() 来中和CurrencyManager 的 BeginEdit()。
它不会阻止每次位置更改时调用 BeginEdit 的成本。
If you have a Bindingsource, you can call EndEdit() on the CurrentChanged Event to neutralize the BeginEdit() of the CurrencyManager.
It won't prevent the cost of calling BeginEdit at every change of position.