Something to keep in mind about INotifyPropertyChanging
is you can't stop the change from happening. This merely allows you to record that the change occurred.
I use it in a framework of mine for change tracking, but it isn't an appropriate method for halting changes.
You could extend your ViewModelBase
with a custom interface/event pair:
delegate void AcceptPendingChangeHandler(
object sender,
AcceptPendingChangeEventArgs e);
interface IAcceptPendingChange
{
AcceptPendingChangeHandler PendingChange;
}
class AcceptPendingChangeEventArgs : EventArgs
{
public string PropertyName { get; private set; }
public object NewValue { get; private set; }
public bool CancelPendingChange { get; set; }
// flesh this puppy out
}
class ViewModelBase : IAcceptPendingChange, ...
{
protected virtual bool RaiseAcceptPendingChange(
string propertyName,
object newValue)
{
var e = new AcceptPendingChangeEventArgs(propertyName, newValue)
var handler = this.PendingChange;
if (null != handler)
{
handler(this, e);
}
return !e.CancelPendingChange;
}
}
At this point you'd need to add it by convention to your view models:
class SomeViewModel : ViewModelBase
{
public string Foo
{
get { return this.foo; }
set
{
if (this.RaiseAcceptPendingChange("Foo", value))
{
this.RaiseNotifyPropertyChanging("Foo");
this.foo = value;
this.RaiseNotifyPropretyChanged("Foo");
}
}
}
}