Browse Source

Merge pull request #4446 from AvaloniaUI/fixes/4403-setter-binding-unsetvalue

Correctly handle UnsetValue/DoNothing in binding setters.
danwalmsley 5 years ago
parent
commit
600ac25931

+ 1 - 1
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<T>.FromUntyped(value);
+            _value = BindingValue<T>.FromUntyped(value);
 
             if (_isActive)
             {

+ 15 - 0
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<object>(AvaloniaProperty.UnsetValue);
+            var descriptor = InstancedBinding.OneWay(subject);
+            var binding = Mock.Of<IBinding>(x => x.Initiate(control, TextBlock.TagProperty, null, false) == descriptor);
+            var style = Mock.Of<IStyle>();
+            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()
         {