WPF:更新绑定源时是否可以自动调用BeginEdit(IEditableObject)?
我有一个实现 IEditableObject 的类,现在我想知道在更新绑定源时是否可以自动调用 BeginEdit() ?
有两种可能的情况:
- 通过数据库填充对象。在这种情况下,我不想调用 BeginEdit()。
- 用户通过输入字段填充对象。在这种情况下,我想在更新源时自动调用 BeginEdit() (我使用双向绑定和 INotifyPropertyChanged)。
我正在考虑在属性更改时调用 BeginEdit() ,但这与第一种情况不太相符,因为我不希望在从数据库填充时调用 BeginEdit() 。
I have a class that implements IEditableObject and now I'm wondering if it's possible to call BeginEdit() automatically when the source of the binding is updated?
There are two possible scenarios:
- Object gets populated via the database. In this case, I don't want to call BeginEdit().
- Object gets populated via the input fields by user. In this case, I would like to call the BeginEdit() automatically when the source is updated (I use two-way binding and INotifyPropertyChanged).
I was considering calling BeginEdit() when the property is changed but that wouldn't go along well with the 1st scenario since I don't want BeginEdit() to be called when populating from database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一种方法来确定对象群体的来源。
enum
可以做到这一点,然后在您的PropertyChanged
中您可以检查导致属性更改的原因,并基于此,您可以调用BeginEdit()
或不。现在,当从数据库更新时,将 Enum 设置为
PopulateSource.Database
。当它因用户更改而发生更改时,您将其设置为PopulateSource.User
。现在,您可以在 PropertyChanged 中检查该变量的状态,从而确定是否调用BeginEdit()
。You need a way to determine the source of the object population. An
enum
might do it and then in yourPropertyChanged
you can check what caused the property to change and based on that, you can callBeginEdit()
or not.Now when updating from database, set your Enum to
PopulateSource.Database
. When it changes because the user changed it, you set it toPopulateSource.User
. Now you can check in your PropertyChanged what the state of this variable is and so determine whether to callBeginEdit()
.