From f4b995f80b13c8075bb7bd061e3986b04e7d9693 Mon Sep 17 00:00:00 2001 From: cabauman Date: Sun, 2 Aug 2020 16:10:42 +0900 Subject: [PATCH 1/2] Fix: BindTo and OneWayBind not working for null values --- .../windows-xaml/PropertyBindingTest.cs | 60 ++++++++++++++++++- .../Property/PropertyBinderImplementation.cs | 2 +- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs b/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs index f6a56e0744..ed8b9b5325 100644 --- a/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs +++ b/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs @@ -235,7 +235,7 @@ public void ViewModelIndexerPropertyToView() } [Fact] - public void BindToShouldntInitiallySetToNull() + public void OneWayBindShouldntInitiallySetToNull() { var vm = new PropertyBindViewModel(); var view = new PropertyBindView { ViewModel = null }; @@ -354,6 +354,64 @@ public void OneWayBindConverter() Assert.False(view.SomeTextBox.IsEnabled); } + [Fact] + public void OneWayBindWithNullStartingValueToNonNullValue() + { + var vm = new PropertyBindViewModel(); + var view = new PropertyBindView { ViewModel = vm }; + + view.OneWayBind(vm, x => x.Property1, x => x.SomeTextBox.Text); + + vm.Property1 = "Baz"; + + Assert.Equal("Baz", view.SomeTextBox.Text); + } + + [Fact] + public void OneWayBindWithNonNullStartingValueToNullValue() + { + var vm = new PropertyBindViewModel(); + var view = new PropertyBindView { ViewModel = vm }; + + vm.Property1 = "Baz"; + + view.OneWayBind(vm, x => x.Property1, x => x.SomeTextBox.Text); + + vm.Property1 = null; + + Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text)); + } + + [Fact] + public void BindToWithNullStartingValueToNonNullValue() + { + var vm = new PropertyBindViewModel(); + var view = new PropertyBindView { ViewModel = vm }; + + view.WhenAnyValue(x => x.ViewModel!.Property1) + .BindTo(view, x => x.SomeTextBox.Text); + + vm.Property1 = "Baz"; + + Assert.Equal("Baz", view.SomeTextBox.Text); + } + + [Fact] + public void BindToWithNonNullStartingValueToNullValue() + { + var vm = new PropertyBindViewModel(); + var view = new PropertyBindView { ViewModel = vm }; + + vm.Property1 = "Baz"; + + view.WhenAnyValue(x => x.ViewModel!.Property1) + .BindTo(view, x => x.SomeTextBox.Text); + + vm.Property1 = null; + + Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text)); + } + [Fact] public void BindExpectsConverterFuncsToNotBeNull() { diff --git a/src/ReactiveUI/Bindings/Property/PropertyBinderImplementation.cs b/src/ReactiveUI/Bindings/Property/PropertyBinderImplementation.cs index 0dc7738718..a339f5c514 100644 --- a/src/ReactiveUI/Bindings/Property/PropertyBinderImplementation.cs +++ b/src/ReactiveUI/Bindings/Property/PropertyBinderImplementation.cs @@ -319,7 +319,7 @@ public IDisposable BindTo( if (converter == null) { - defaultSetter?.Invoke(paramTarget, paramValue ?? throw new ArgumentNullException(nameof(paramValue)), paramParams); + defaultSetter?.Invoke(paramTarget, paramValue, paramParams); return defaultGetter(paramTarget, paramParams); } From 838beaa97d5474b34bb1a077b07b1220ac869adb Mon Sep 17 00:00:00 2001 From: cabauman Date: Mon, 3 Aug 2020 01:39:17 +0900 Subject: [PATCH 2/2] Add OneWayBind test using selector parameter --- .../Platforms/windows-xaml/PropertyBindingTest.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs b/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs index ed8b9b5325..b3e725d165 100644 --- a/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs +++ b/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs @@ -382,6 +382,19 @@ public void OneWayBindWithNonNullStartingValueToNullValue() Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text)); } + [Fact] + public void OneWayBindWithSelectorAndNonNullStartingValueToNullValue() + { + var vm = new PropertyBindViewModel(); + var view = new PropertyBindView { ViewModel = vm }; + + view.OneWayBind(vm, x => x.Model, x => x.SomeTextBox.Text, x => x?.AnotherThing); + + vm.Model = null; + + Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text)); + } + [Fact] public void BindToWithNullStartingValueToNonNullValue() {