Search code examples
c#wpfxamlidataerrorinfo

How do you validate a clr property after it has been updated instead of before when implementing IDataErrorInfo?


I have implemented IDataErrorInfo in one of my classes to validate a property of that class. The property is bound to a control on my wpf user control. The validataion works fine, except there is one vital flaw.

It seems to be calling the IDataErrorInfo member public string this[string columnName] before the property is updated, so when I check the value of the property being changed it contains the previous value not the one just entered. Therefore, the validation is always one step behind. This means that I can't check for incorrect values that have just been entered.

Is there any way of forcing this validation to be called after the property has been updated and not before. I have tried changing the UpdateSourceTrigger to both LostFocus and PropertyChanged but they still report the previous value, just at different times.

Thanks.


Solution

  • The behavior that you are referring to can be reproduced when property change notifications are sent before the property is assigned.

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged
                    (this, new PropertyChangedEventArgs("FirstName"));
            }
            _firstName = value;
        }
    }
    

    May be calling the change notification after you have actually set the value may do the trick. And just for the record I don't call property changed notification like this!