Browse Source

Merge pull request #10497 from AvaloniaUI/fixes/setcurrent-value-with-style

Fix `SetCurrent` value in combination with certain styles
pull/10530/head
Steven Kirk 3 years ago
committed by GitHub
parent
commit
e9cffd02be
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.Base/PropertyStore/ValueStore.cs
  2. 81
      tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetCurrentValue.cs

2
src/Avalonia.Base/PropertyStore/ValueStore.cs

@ -924,7 +924,7 @@ namespace Avalonia.PropertyStore
{
_effectiveValues.GetKeyValue(i, out var key, out var e);
if (e.Priority == BindingPriority.Unset)
if (e.Priority == BindingPriority.Unset && !e.IsOverridenCurrentValue)
{
RemoveEffectiveValue(key, i);
e.DisposeAndRaiseUnset(this, key);

81
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetCurrentValue.cs

@ -1,6 +1,9 @@
using System;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Diagnostics;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Xunit;
using Observable = Avalonia.Reactive.Observable;
@ -275,6 +278,79 @@ namespace Avalonia.Base.UnitTests
Assert.Equal("style", target.Foo);
}
[Fact]
public void SetCurrent_Value_Persists_When_Toggling_Style_1()
{
var target = new Class1();
var root = new TestRoot(target)
{
Styles =
{
new Style(x => x.OfType<Class1>().Class("foo"))
{
Setters = { new Setter(Class1.BarProperty, "bar") },
}
}
};
root.LayoutManager.ExecuteInitialLayoutPass();
target.SetCurrentValue(Class1.FooProperty, "current");
Assert.Equal("current", target.Foo);
Assert.Equal("bardefault", target.Bar);
target.Classes.Add("foo");
Assert.Equal("current", target.Foo);
Assert.Equal("bar", target.Bar);
target.Classes.Remove("foo");
Assert.Equal("current", target.Foo);
Assert.Equal("bardefault", target.Bar);
}
[Fact]
public void SetCurrent_Value_Persists_When_Toggling_Style_2()
{
var target = new Class1();
var root = new TestRoot(target)
{
Styles =
{
new Style(x => x.OfType<Class1>().Class("foo"))
{
Setters =
{
new Setter(Class1.BarProperty, "bar"),
new Setter(Class1.InheritedProperty, "inherited"),
},
}
}
};
root.LayoutManager.ExecuteInitialLayoutPass();
target.SetCurrentValue(Class1.FooProperty, "current");
Assert.Equal("current", target.Foo);
Assert.Equal("bardefault", target.Bar);
Assert.Equal("inheriteddefault", target.Inherited);
target.Classes.Add("foo");
Assert.Equal("current", target.Foo);
Assert.Equal("bar", target.Bar);
Assert.Equal("inherited", target.Inherited);
target.Classes.Remove("foo");
Assert.Equal("current", target.Foo);
Assert.Equal("bardefault", target.Bar);
Assert.Equal("inheriteddefault", target.Inherited);
}
private BindingPriority GetPriority(AvaloniaObject target, AvaloniaProperty property)
{
return target.GetDiagnostic(property).Priority;
@ -285,16 +361,19 @@ namespace Avalonia.Base.UnitTests
return target.GetDiagnostic(property).IsOverriddenCurrentValue;
}
private class Class1 : AvaloniaObject
private class Class1 : Control
{
public static readonly StyledProperty<string> FooProperty =
AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
public static readonly StyledProperty<string> BarProperty =
AvaloniaProperty.Register<Class1, string>(nameof(Bar), "bardefault");
public static readonly StyledProperty<string> InheritedProperty =
AvaloniaProperty.Register<Class1, string>(nameof(Inherited), "inheriteddefault", inherits: true);
public static readonly StyledProperty<double> CoercedProperty =
AvaloniaProperty.Register<Class1, double>(nameof(Coerced), coerce: Coerce);
public string Foo => GetValue(FooProperty);
public string Bar => GetValue(BarProperty);
public string Inherited => GetValue(InheritedProperty);
public double Coerced => GetValue(CoercedProperty);
public double CoerceMax { get; set; } = 100;

Loading…
Cancel
Save