diff --git a/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs b/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs index 20d708b20e..1d8f6b8408 100644 --- a/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs +++ b/src/Avalonia.Base/PropertyStore/EffectiveValue`1.cs @@ -15,22 +15,19 @@ namespace Avalonia.PropertyStore /// internal sealed class EffectiveValue : EffectiveValue { + private readonly StyledPropertyMetadata _metadata; private T? _baseValue; private UncommonFields? _uncommon; - public EffectiveValue( - AvaloniaObject owner, - StyledPropertyBase property, - T value, - BindingPriority priority) + public EffectiveValue(AvaloniaObject owner, StyledPropertyBase property) { - Value = value; - Priority = priority; + Priority = BindingPriority.Unset; BasePriority = BindingPriority.Unset; + _metadata = property.GetMetadata(owner.GetType()); - if (property.HasCoercion && - property.GetMetadata(owner.GetType()) is { } metadata && - metadata.CoerceValue is { } coerce) + var value = _metadata.DefaultValue; + + if (property.HasCoercion && _metadata.CoerceValue is { } coerce) { _uncommon = new() { @@ -39,7 +36,11 @@ namespace Avalonia.PropertyStore _uncoercedBaseValue = value, }; - value = coerce(owner, value); + Value = coerce(owner, value); + } + else + { + Value = value; } } @@ -82,8 +83,8 @@ namespace Avalonia.PropertyStore Debug.Assert(oldValue is not null || newValue is not null); var p = (StyledPropertyBase)property; - var o = oldValue is not null ? ((EffectiveValue)oldValue).Value : p.GetDefaultValue(owner.GetType()); - var n = newValue is not null ? ((EffectiveValue)newValue).Value : p.GetDefaultValue(owner.GetType()); + var o = oldValue is not null ? ((EffectiveValue)oldValue).Value : _metadata.DefaultValue; + var n = newValue is not null ? ((EffectiveValue)newValue).Value : _metadata.DefaultValue; var priority = newValue is not null ? BindingPriority.Inherited : BindingPriority.Unset; if (!EqualityComparer.Default.Equals(o, n)) @@ -131,7 +132,7 @@ namespace Avalonia.PropertyStore } else { - oldValue = property.GetDefaultValue(owner.GetType()); + oldValue = _metadata.DefaultValue; priority = BindingPriority.Unset; } diff --git a/src/Avalonia.Base/PropertyStore/ValueStore.cs b/src/Avalonia.Base/PropertyStore/ValueStore.cs index bbc71f1a38..8790991182 100644 --- a/src/Avalonia.Base/PropertyStore/ValueStore.cs +++ b/src/Avalonia.Base/PropertyStore/ValueStore.cs @@ -182,8 +182,7 @@ namespace Avalonia.PropertyStore } else { - var defaultValue = property.GetDefaultValue(Owner.GetType()); - var effectiveValue = new EffectiveValue(Owner, property, defaultValue, BindingPriority.Unset); + var effectiveValue = new EffectiveValue(Owner, property); AddEffectiveValue(property, effectiveValue); effectiveValue.SetAndRaise(this, result, priority); } @@ -199,8 +198,7 @@ namespace Avalonia.PropertyStore } else { - var defaultValue = property.GetDefaultValue(Owner.GetType()); - var effectiveValue = new EffectiveValue(Owner, property, defaultValue, BindingPriority.Unset); + var effectiveValue = new EffectiveValue(Owner, property); AddEffectiveValue(property, effectiveValue); effectiveValue.SetLocalValueAndRaise(this, property, value); } diff --git a/src/Avalonia.Base/StyledPropertyBase.cs b/src/Avalonia.Base/StyledPropertyBase.cs index 90b724bee6..6fb2f79918 100644 --- a/src/Avalonia.Base/StyledPropertyBase.cs +++ b/src/Avalonia.Base/StyledPropertyBase.cs @@ -183,7 +183,7 @@ namespace Avalonia internal override EffectiveValue CreateEffectiveValue(AvaloniaObject o) { - return new EffectiveValue(o, this, GetDefaultValue(o.GetType()), BindingPriority.Unset); + return new EffectiveValue(o, this); } ///