Possible minor HasPropertyChanged bug?

Jul 6, 2011 at 4:18 AM
Edited Jul 6, 2011 at 7:47 AM

Not sure if this is intentional, or I'm doing something silly with edit modes but thought it was worth asking...

The DataWrapperSupportingBase.HasPropertyChanged method treats null strings as being different to empty strings. This means if you have say, a textbox bound to a DataWrapper then enter some text, clear the text and click another control (to update the binding), the DataWrapper.IsDirty property will now be true (since its DataValue is an empty string and its _savedState value is null) even though from the users point of view it hasn't changed and so should not be dirty.

This code changes it so that HasPropertyChanged will coalesce null's to string.empty before doing the comparison:

 

        public bool HasPropertyChanged(string propertyName)
        {
            string _propertyName = propertyName.ToLower();

            if (_savedState == null)
                return false;

            object saveValue;
            object currentValue;

            if (!_savedState.TryGetValue(_propertyName, out saveValue) ||
            !this.GetFieldValues().TryGetValue(_propertyName, out currentValue))
                return false;

            if (saveValue == null || currentValue == null)
            {
                //Here's the fix
                if (saveValue is string || currentValue is string)
                {
                    return (string)(saveValue ?? string.Empty) != (string)(currentValue ?? string.Empty);
                }
                else
                {
                    return saveValue != currentValue;
                }
            }

            return !saveValue.Equals(currentValue);


        }

 


 

 

DataWrapperDirtySupportingBase
Jul 6, 2011 at 7:44 PM

Sensible idea, have changed this to use your improved code. Cheers