From 15896f3158082a32100d31bce077e3fcfbfd339e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 6 Jan 2017 12:15:06 +0100 Subject: [PATCH 1/2] Added failing test for #831. --- .../Data/ExpressionObserverTests_SetValue.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Markup.UnitTests/Data/ExpressionObserverTests_SetValue.cs b/tests/Avalonia.Markup.UnitTests/Data/ExpressionObserverTests_SetValue.cs index 3238435841..0705ae9c5a 100644 --- a/tests/Avalonia.Markup.UnitTests/Data/ExpressionObserverTests_SetValue.cs +++ b/tests/Avalonia.Markup.UnitTests/Data/ExpressionObserverTests_SetValue.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Collections.Generic; using System.Reactive.Linq; +using System.Reactive.Subjects; using Avalonia.Markup.Data; using Avalonia.UnitTests; using Xunit; @@ -54,6 +54,28 @@ namespace Avalonia.Markup.UnitTests.Data Assert.False(target.SetValue("foo")); } + /// + /// Test for #831 - Bound properties are incorrectly updated when changing tab items. + /// + /// + /// There was a bug whereby pushing a null as the ExpressionObserver root didn't update + /// the leaf node, cauing a subsequent SetValue to update an object that should have become + /// unbound. + /// + [Fact] + public void Pushing_Null_To_RootObservable_Updates_Leaf_Node() + { + var data = new Class1 { Foo = new Class2 { Bar = "bar" } }; + var rootObservable = new BehaviorSubject(data); + var target = new ExpressionObserver(rootObservable, "Foo.Bar"); + + target.Subscribe(_ => { }); + rootObservable.OnNext(null); + target.SetValue("baz"); + + Assert.Equal("bar", data.Foo.Bar); + } + private class Class1 : NotifyingBase { private Class2 _foo; From f4f0597dca4a06afab5ddeb704f4c9eb50eb5e49 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 6 Jan 2017 12:56:36 +0100 Subject: [PATCH 2/2] Update next node in binding chain on error. Fixes #831 and makes `Pushing_Null_To_RootObservable_Updates_Leaf_Node` test pass. --- .../Avalonia.Markup/Data/ExpressionNode.cs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Markup/Avalonia.Markup/Data/ExpressionNode.cs b/src/Markup/Avalonia.Markup/Data/ExpressionNode.cs index 93f20e4c77..56c0072eaa 100644 --- a/src/Markup/Avalonia.Markup/Data/ExpressionNode.cs +++ b/src/Markup/Avalonia.Markup/Data/ExpressionNode.cs @@ -131,20 +131,14 @@ namespace Avalonia.Markup.Data } else { - if (notification.Error != null) + if (Next != null) { - _observer.OnNext(notification); + Next.Target = new WeakReference(notification.Value); } - else if (notification.HasValue) + + if (Next == null || notification.Error != null) { - if (Next != null) - { - Next.Target = new WeakReference(notification.Value); - } - else - { - _observer.OnNext(value); - } + _observer.OnNext(value); } } }