From c7d565fd0ca12af45ffa99a44b8e9977a1be74e9 Mon Sep 17 00:00:00 2001 From: MonkAlex <3amepob@gmail.com> Date: Sun, 5 Aug 2018 11:11:39 +0400 Subject: [PATCH 1/2] #1798 Fix binding checkbox with three state. --- src/Avalonia.Controls/Primitives/ToggleButton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Primitives/ToggleButton.cs b/src/Avalonia.Controls/Primitives/ToggleButton.cs index dc9b70ab8c..1d2e2f2100 100644 --- a/src/Avalonia.Controls/Primitives/ToggleButton.cs +++ b/src/Avalonia.Controls/Primitives/ToggleButton.cs @@ -14,7 +14,7 @@ namespace Avalonia.Controls.Primitives nameof(IsChecked), o => o.IsChecked, (o, v) => o.IsChecked = v, - unsetValue: false, + unsetValue: null, defaultBindingMode: BindingMode.TwoWay); public static readonly StyledProperty IsThreeStateProperty = From e69f3c7f85f2a751ef9f0a343c5a38753f204714 Mon Sep 17 00:00:00 2001 From: MonkAlex <3amepob@gmail.com> Date: Sun, 5 Aug 2018 13:36:32 +0400 Subject: [PATCH 2/2] #1798 Test for binding checkbox with three state. --- .../Primitives/ToggleButtonTests.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs index ab74d5e7d6..4f4ab47b0a 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs @@ -44,15 +44,41 @@ namespace Avalonia.Controls.Primitives.UnitTests Assert.False(toggleButton.IsChecked); } + [Fact] + public void ToggleButton_ThreeState_Checked_Binds_To_Nullable_Bool() + { + var threeStateButton = new ToggleButton(); + var source = new Class1(); + + threeStateButton.DataContext = source; + threeStateButton.Bind(ToggleButton.IsCheckedProperty, new Binding(nameof(Class1.NullableFoo))); + + source.NullableFoo = true; + Assert.True(threeStateButton.IsChecked); + + source.NullableFoo = false; + Assert.False(threeStateButton.IsChecked); + + source.NullableFoo = null; + Assert.Null(threeStateButton.IsChecked); + } + private class Class1 : NotifyingBase { private bool _foo; + private bool? nullableFoo; public bool Foo { get { return _foo; } set { _foo = value; RaisePropertyChanged(); } } + + public bool? NullableFoo + { + get { return nullableFoo; } + set { nullableFoo = value; RaisePropertyChanged(); } + } } } }