From 472352558f24c7be59c9bfd86a6129d600ae6e82 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 6 Aug 2020 17:13:34 +0200 Subject: [PATCH 1/2] Added failing test for #4403. --- tests/Avalonia.Styling.UnitTests/SetterTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Avalonia.Styling.UnitTests/SetterTests.cs b/tests/Avalonia.Styling.UnitTests/SetterTests.cs index 46cde30b8a..901233801e 100644 --- a/tests/Avalonia.Styling.UnitTests/SetterTests.cs +++ b/tests/Avalonia.Styling.UnitTests/SetterTests.cs @@ -35,6 +35,21 @@ namespace Avalonia.Styling.UnitTests Assert.Equal("foo", control.Text); } + [Fact] + public void Setter_Should_Handle_Binding_Producing_UnsetValue() + { + var control = new TextBlock(); + var subject = new BehaviorSubject(AvaloniaProperty.UnsetValue); + var descriptor = InstancedBinding.OneWay(subject); + var binding = Mock.Of(x => x.Initiate(control, TextBlock.TagProperty, null, false) == descriptor); + var style = Mock.Of(); + var setter = new Setter(TextBlock.TagProperty, binding); + + setter.Instance(control).Start(false); + + Assert.Equal("", control.Text); + } + [Fact] public void Setter_Should_Materialize_Template_To_Property() { From de8171f8ac2221095bd9129636d754194992884b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 6 Aug 2020 17:13:49 +0200 Subject: [PATCH 2/2] Correctly handle UnsetValue/DoNothing in setters. Just need to call `BindingValue.FromUntyped` instead of doing a conditional, because otherwise we run the risk of passing `UnsetValue` to the `BindingValue` ctor which will throw in that case. --- src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs b/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs index e177993d13..929f7142bb 100644 --- a/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs +++ b/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs @@ -164,7 +164,7 @@ namespace Avalonia.Styling private void ConvertAndPublishNext(object? value) { - _value = value is T v ? v : BindingValue.FromUntyped(value); + _value = BindingValue.FromUntyped(value); if (_isActive) {