diff --git a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs index c3152ed01a..8a335d24dc 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs @@ -347,6 +347,58 @@ namespace Avalonia.Base.UnitTests.Styling Assert.Equal(Brushes.Green, data.Bar); } + [Fact] + public void Non_Active_Styled_Property_Setter_With_TwoWay_Binding_Should_Not_Update_Source() + { + var data = new Data { Bar = Brushes.Red }; + var control = new Border + { + DataContext = data, + }; + + var style1 = new Style(x => x.OfType()) + { + Setters = + { + new Setter + { + Property = Border.BackgroundProperty, + Value = new Binding + { + Path = "Bar", + Mode = BindingMode.TwoWay + } + } + }, + }; + + var style2 = new Style(x => x.OfType().Class("foo")) + { + Setters = + { + new Setter + { + Property = Border.BackgroundProperty, + Value = Brushes.Green, + } + }, + }; + + Apply(style1, control); + Apply(style2, control); + + // `style1` is initially active. + Assert.Equal(Brushes.Red, control.Background); + + // Activate `style2`. + control.Classes.Add("foo"); + Assert.Equal(Brushes.Green, control.Background); + + // The two-way binding from `style1` is now inactive and so should not write back to + // the DataContext. + Assert.Equal(Brushes.Red, data.Bar); + } + private void Apply(Style style, Control control) { style.TryAttach(control, null);