views:

292

answers:

1

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.

A: 

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().

Tony