我的绑定源可以告诉我是否发生了更改吗?
我有一个 BindingSource
我在 winforms 数据绑定中使用,当用户在更改数据后尝试关闭表单时,我希望有某种提示。类似于“您确定要退出而不保存更改吗?”
我知道我可以通过 BindingSource
的 CurrencyManager.ItemChanged
事件只需翻转“已更改”布尔值即可。
但是,我想要更强大的功能。 我想知道当前数据何时与原始数据不同。该事件只是告诉我是否发生了变化。用户仍然可以更改属性,点击撤消,并且我仍然认为数据中有更改需要保存。
我想模仿记事本的类似功能
- 打开记事本
- 类型
- 删除所有内容(本质上撤消您所做的事情)
- 关闭记事本,记事本关闭,不提示保存更改,因为它知道结束状态==初始状态
如果这是不可能的,那么我应该使用上面概述的 ItemChanged
事件处理程序还是有更好的方法?
作为记录,我正在寻找类似的东西,
bool HasChanged()
{
return this.currentState != this.initialState;
}
我
bool HasChanged()
{
// this._hasChanged is set to true via event handlers
return this._hasChanged;
}
只是不想自己管理当前状态和初始状态,我正在寻找一种从 获取该信息的方法BindingSource
如果我可以从 BindingSource
获取此功能,则更加理想,因为我将能够在许多不同的数据源上使用该功能,无论类型如何,等等。
I have a BindingSource
that I'm using in winforms data binding and I'd like to have some sort of prompt for when the user attempts to close the form after they've made changes to the data. A sort of "Are you sure you want to exit without saving changes?"
I'm aware that I can do this via the BindingSource
's CurrencyManager.ItemChanged
event by just flipping a "has changed" boolean.
However, I want a more robust functionality. I'd like to know when the current data is different from the original data. The event just tells me if somethings changed. A user could still change a property, hit undo, and I would still think that there is a change in the data to save.
I want to mimic this similar functionality of notepad
- open notepad
- type something
- delete everything (essentially undoing what you did)
- close notepad, notepad closes, no prompt to save changes because it knows the end state == the initial state
If this is not possible, then should I go with the ItemChanged
event handler as outlined above or is there a better way?
For the record, I'm looking for something along the lines of
bool HasChanged()
{
return this.currentState != this.initialState;
}
not this
bool HasChanged()
{
// this._hasChanged is set to true via event handlers
return this._hasChanged;
}
I'd just rather not have to manage the current state and the initial state myself, I'm looking for a way to grab that info from the BindingSource
If I can get this functionality from the BindingSource
its way more ideal since I will be able to use the functionality on many different data sources, regardless of type, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须从对象类中实现
INotifyPropertyChanged
接口,然后通过DataSource
BindingSource 中的类型类的正确事件处理程序捕获发生的更改
属性。提供您所需的一个对象是
DataSet
,它包含持久实体的原始状态和当前(已更改)状态。然后,当取消时,您只需调用Rollback()
方法即可。当接受更改时,将调用AcceptChanges()
方法。除了
DataSet
之外,也许考虑像NHibernate这样的ORM可以为您完成这项工作,并且允许您使用自定义定义的对象,而不是DataSet
。在表单中保持ISession
API 处于活动状态将允许 ISession 跟踪您的更改,无论它是什么对象,只要 NHibernate 知道它即可。实现 INotifyPropertyChanged 接口的另一个解决方案是在属性设置器中,您可以在私有字段中或为对象的每个属性存储原始值。您可以简单地使用一个带有
HasChanges
属性的抽象类来返回每个属性是否为其原始状态,然后相应地返回 true 或 false。我有一个关于我们有趣的初步讨论的问题。我只是想确定一件事。如果我们愿意的话,我们可以称之为语言障碍。但是通过
INotifyPropertyChanged
接口发布PropertyChanged
事件也会以某种方式将对象“回滚”到其原始状态。您必须注意的唯一细节是,如果用户说他不想保留更改,则通过BackgroundWorker
类从底层数据库重新加载此 CurrentItem 并完成! GUI 没有滞后,您的用户已取消更改,并且您将对象重置为其默认/原始状态!好吧,我想这里有足够的细节让你自己产生想法,再加上其他人提供的所有其他好的答案。我相信您会找到实现您想要的目标的方法。
祝您成功! =)
You'll have to implement the
INotifyPropertyChanged
interface from within your object classes, then catch whenever a change occurs through proper event handlers for your type class within yourDataSource
BindingSource
property.The one object offering what you require is the
DataSet
, containing both the Original and Current (changed) state of an persistent entity. Then, when one cancels, all you need to call is theRollback()
method. When one accepts the changes, then a call to theAcceptChanges()
method will do.Besides the
DataSet
, perhaps considering an ORM like NHibernate will do the job for you, plus allowing you to use custom defined objects, instead of aDataSet
. Keeping theISession
API alive while in your form will allow the ISession to keep track of your changes whatever it may be to whatever object it is, as long as it is know by NHibernate.Another solution implementing the
INotifyPropertyChanged
interface, is at the property setter, you could stock the Original value within a private field or for every property of an object. You could simple have an abstract class with theHasChanges
property return whether each property is as its Original state, then return true or false accordingly.I have a question regarding our interesting initial discussion. I just want to make sure of one thing. Let's call it language barrier if we like. But publishing the
PropertyChanged
event through theINotifyPropertyChanged
interface will also somehow "rollback" an object to its original state. The only detail you had to take care is that if the user says he doesn't want to keep the changes, then reload this CurrentItem from the underlying database via theBackgroundWorker
class and its done! No lagging from your GUI, your user has canceled the changes, and you resetted the object to its default/original state!Well, I guess here's enough details to make yourself an idea, plus all of the other good answers provided by the others. I am confident you will find your way to accomplish what you want.
Best of success! =)
威尔是对的,您应该实施 < code>INotifyPropertyChanged,最好与
IDataInfoError
为您的用户获取可见信息。为了让您的对象获得编辑状态和通知,请尝试使用
IEditableObject
接口。WinForms 默认使用所有三个接口,有助于使程序员的工作更轻松。
Will is right, you should implement
INotifyPropertyChanged
, ideally in conjunction withIDataInfoError
to get visisble information for your users.For your Objects to get a state and a notification on Editing, try using the
IEditableObject
interface.All three interfaces are used by default from WinForms and help make the programmers life easier.
您可以根据初始状态的快照检查状态,而不是稍微翻转。
Instead of flipping a bit, you could check the state against a snapshot of your initial state.
当您打开详细信息时,您可以克隆要修改的实体。
然后,当用户尝试关闭表单时,您可以将克隆(原始状态的实体)与修改(或未修改)的实体进行比较。如果克隆和实体不相等,您可以提示用户。
When you open your detail, you could make a clone of the entity that you are going to modify.
Then, when the user attempts to close the form, you could compare the clone (the entity in its original state) with the modified (or not) entity. If the clone and the entity are not equals, you could prompt the user.
您可以推出自己的绑定源并实现它来执行您想要的操作,这样您就不需要在每个表单上处理
INotifyChange
- 您只需让BindingSource
为您提供更改后的内容元素 - 当BindingSource
更新时,此功能有效 - 可绑定控件.UpdateSourceTrigger
设置为UpdateOnPropertyChanged
。是即时的(几乎是)。这里有一些可以帮助您入门的东西 - 我几年前在网上找到了它,我不记得代码的创始人,我为了我的目的对其进行了轻微的修改。
You could roll your own binding source and implement it to do what you want that way you do not need
INotifyChange
handling on every form - you just let theBindingSource
give you the changed element - this works when theBindingSource
is updated - bindable control.UpdateSourceTrigger
is set toUpdateOnPropertyChanged
. is instant(well almost).Here is something to get you started - I found it on the net years ago I do not remember the originator of the code , I have modified it slightly for my purpose.
是的,但是需要一些工作。我知道,这是一个迟到的答案,但我最近问了自己同样的问题,并提出了以下解决方案,我将其包装到类
UpdateManager
中。到目前为止,我只考虑了与单个对象的绑定。这适用于普通 POCO 对象。 不需要实现
INotifyPropertyChanged
;但是,只有通过 UI 进行更改时它才有效。不会检测到通过业务对象中的代码进行的更改。但这在大多数情况下足以检测对象是否脏或处于已保存状态。在我这样使用的表单中:
每当
Dirty
状态发生变化时,都会在输出窗口中打印“Dirty”或“Saved”。Yes, but there is some work involved. I know, it's a late answer, but I asked myself the same question resecently and came up with the following soltion that I wrapped up into the class
UpdateManager
. I only accounted for binding to a single object so far.This works with plain POCO objects. Implementing
INotifyPropertyChanged
is not required; however, it works only if the changes are made through the UI. Changes made through code in the business object are not detected. But this enough in most cases to detect whether the object is dirty or in a saved state.In the form I used it like this:
Whenever the
Dirty
status changes, this prints either "Dirty" or "Saved" in the Output window.