diff --git a/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs b/src/ReactiveUI.Tests/Platforms/windows-xaml/PropertyBindingTest.cs index f6a56e0744..b3e725d165 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,77 @@ 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 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() + { + 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); }