WPF:更新绑定源时是否可以自动调用BeginEdit(IEditableObject)?

发布于 2024-08-22 01:13:06 字数 310 浏览 5 评论 0原文

我有一个实现 IEditableObject 的类,现在我想知道在更新绑定源时是否可以自动调用 BeginEdit() ?

有两种可能的情况:

  1. 通过数据库填充对象。在这种情况下,我不想调用 BeginEdit()。
  2. 用户通过输入字段填充对象。在这种情况下,我想在更新源时自动调用 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:

  1. Object gets populated via the database. In this case, I don't want to call BeginEdit().
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2024-08-29 01:13:06

您需要一种方法来确定对象群体的来源。 enum 可以做到这一点,然后在您的 PropertyChanged 中您可以检查导致属性更改的原因,并基于此,您可以调用 BeginEdit() 或不。

Enum PopulateSource
{
   Database = 0,
   User
}

现在,当从数据库更新时,将 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 your PropertyChanged you can check what caused the property to change and based on that, you can call BeginEdit() or not.

Enum PopulateSource
{
   Database = 0,
   User
}

Now when updating from database, set your Enum to PopulateSource.Database. When it changes because the user changed it, you set it to PopulateSource.User. Now you can check in your PropertyChanged what the state of this variable is and so determine whether to call BeginEdit().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文