Browse Source

Merge pull request #1799 from MonkAlex/master

#1798 Fix binding checkbox with three state.
pull/1804/head
Nelson Carrillo 8 years ago
committed by GitHub
parent
commit
93833ea586
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.Controls/Primitives/ToggleButton.cs
  2. 26
      tests/Avalonia.Controls.UnitTests/Primitives/ToggleButtonTests.cs

2
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<bool> IsThreeStateProperty =

26
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(); }
}
}
}
}

Loading…
Cancel
Save