Skip to content

Commit

Permalink
Merge pull request #10497 from AvaloniaUI/fixes/setcurrent-value-with…
Browse files Browse the repository at this point in the history
…-style

Fix `SetCurrent` value in combination with certain styles
  • Loading branch information
grokys authored Mar 1, 2023
2 parents 24ad4af + baf2a69 commit e9cffd0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.Base/PropertyStore/ValueStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ private void ReevaluateEffectiveValues()
{
_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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -275,6 +278,79 @@ public void StyleTrigger_Binding_Overrides_CurrentValue_With_Style_Priority()
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;
Expand All @@ -285,16 +361,19 @@ private bool IsOverridden(AvaloniaObject target, AvaloniaProperty property)
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;
Expand Down

0 comments on commit e9cffd0

Please sign in to comment.