Browse Source

Merge pull request #3174 from mstr2/validate-fix

Fixed an issue where ValueStore.Changed could be called with pre-vali…
pull/3196/head
danwalmsley 7 years ago
committed by GitHub
parent
commit
c3f972cd78
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/Avalonia.Base/ValueStore.cs
  2. 12
      tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Validation.cs

12
src/Avalonia.Base/ValueStore.cs

@ -57,7 +57,8 @@ namespace Avalonia
{ {
if (priority == (int)BindingPriority.LocalValue) if (priority == (int)BindingPriority.LocalValue)
{ {
_propertyValues.SetValue(property, Validate(property, value)); Validate(property, ref value);
_propertyValues.SetValue(property, value);
Changed(property, priority, v, value); Changed(property, priority, v, value);
return; return;
} }
@ -78,7 +79,8 @@ namespace Avalonia
if (priority == (int)BindingPriority.LocalValue) if (priority == (int)BindingPriority.LocalValue)
{ {
_propertyValues.AddValue(property, Validate(property, value)); Validate(property, ref value);
_propertyValues.AddValue(property, value);
Changed(property, priority, AvaloniaProperty.UnsetValue, value); Changed(property, priority, AvaloniaProperty.UnsetValue, value);
return; return;
} }
@ -166,16 +168,14 @@ namespace Avalonia
validate2); validate2);
} }
private object Validate(AvaloniaProperty property, object value) private void Validate(AvaloniaProperty property, ref object value)
{ {
var validate = ((IStyledPropertyAccessor)property).GetValidationFunc(_owner.GetType()); var validate = ((IStyledPropertyAccessor)property).GetValidationFunc(_owner.GetType());
if (validate != null && value != AvaloniaProperty.UnsetValue) if (validate != null && value != AvaloniaProperty.UnsetValue)
{ {
return validate(_owner, value); value = validate(_owner, value);
} }
return value;
} }
private DeferredSetter<T> GetDeferredSetter<T>(AvaloniaProperty property) private DeferredSetter<T> GetDeferredSetter<T>(AvaloniaProperty property)

12
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Validation.cs

@ -78,6 +78,18 @@ namespace Avalonia.Base.UnitTests
Assert.Equal(10, target.GetValue(Class1.AttachedProperty)); Assert.Equal(10, target.GetValue(Class1.AttachedProperty));
} }
[Fact]
public void PropertyChanged_Event_Uses_Coerced_Value()
{
var inst = new Class1();
inst.PropertyChanged += (sender, e) =>
{
Assert.Equal(10, e.NewValue);
};
inst.SetValue(Class1.QuxProperty, 15);
}
private class Class1 : AvaloniaObject private class Class1 : AvaloniaObject
{ {
public static readonly StyledProperty<int> QuxProperty = public static readonly StyledProperty<int> QuxProperty =

Loading…
Cancel
Save