diff --git a/src/Controls/src/Core/CheckBox/CheckBox.cs b/src/Controls/src/Core/CheckBox/CheckBox.cs index 32856c6f4cbf..f13ab47ff78e 100644 --- a/src/Controls/src/Core/CheckBox/CheckBox.cs +++ b/src/Controls/src/Core/CheckBox/CheckBox.cs @@ -1,6 +1,8 @@ #nullable disable using System; using System.Diagnostics; +using System.Windows.Input; +using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Graphics; namespace Microsoft.Maui.Controls @@ -8,7 +10,7 @@ namespace Microsoft.Maui.Controls /// [DebuggerDisplay("{GetDebuggerDisplay(), nq}")] [ElementHandler] - public partial class CheckBox : View, IElementConfiguration, IBorderElement, IColorElement, ICheckBox + public partial class CheckBox : View, IElementConfiguration, IBorderElement, IColorElement, ICheckBox, ICommandElement { readonly Lazy> _platformConfigurationRegistry; /// @@ -19,11 +21,45 @@ public partial class CheckBox : View, IElementConfiguration, IBorderEl BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), false, propertyChanged: (bindable, oldValue, newValue) => { - ((CheckBox)bindable).Handler?.UpdateValue(nameof(ICheckBox.Foreground)); - ((CheckBox)bindable).CheckedChanged?.Invoke(bindable, new CheckedChangedEventArgs((bool)newValue)); - ((CheckBox)bindable).ChangeVisualState(); + if (bindable is not CheckBox checkBox) + { + return; + } + + checkBox.Handler?.UpdateValue(nameof(ICheckBox.Foreground)); + checkBox.CheckedChanged?.Invoke(bindable, new CheckedChangedEventArgs((bool)newValue)); + if (checkBox.Command?.CanExecute(checkBox.CommandParameter) == true) + { + checkBox.Command.Execute(checkBox.CommandParameter); + } + + checkBox.ChangeVisualState(); }, defaultBindingMode: BindingMode.TwoWay); + /// Bindable property for the property. + public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(CheckBox), null, propertyChanging: CommandElement.OnCommandChanging, propertyChanged: CommandElement.OnCommandChanged); + + /// Bindable property for the property. + public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(CheckBox), null, propertyChanged: CommandElement.OnCommandParameterChanged); + + /// + /// Gets or sets the command that is executed when the CheckBox is checked or unchecked. This is a bindable property. + /// + public ICommand Command + { + get => (ICommand)GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + /// + /// Gets or sets the parameter to pass to the when it is executed. This is a bindable property. + /// + public object CommandParameter + { + get => GetValue(CommandParameterProperty); + set => SetValue(CommandParameterProperty, value); + } + /// Bindable property for . public static readonly BindableProperty ColorProperty = ColorElement.ColorProperty; @@ -105,6 +141,11 @@ void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue) bool IBorderElement.IsBackgroundSet() => IsSet(BackgroundProperty); bool IBorderElement.IsBorderColorSet() => false; bool IBorderElement.IsBorderWidthSet() => false; + void ICommandElement.CanExecuteChanged(object sender, EventArgs e) => + RefreshIsEnabledProperty(); + + protected override bool IsEnabledCore => + base.IsEnabledCore && CommandElement.GetCanExecute(this); public Paint Foreground => Color?.AsPaint(); bool ICheckBox.IsChecked @@ -113,6 +154,12 @@ bool ICheckBox.IsChecked set => SetValue(IsCheckedProperty, value, SetterSpecificity.FromHandler); } + ICommand ICommandElement.Command => Command; + + object ICommandElement.CommandParameter => CommandParameter; + + WeakCommandSubscription ICommandElement.CleanupTracker { get; set; } + private protected override string GetDebuggerDisplay() { return $"{base.GetDebuggerDisplay()}, IsChecked = {IsChecked}"; diff --git a/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt index 218f4c597de5..1081d465a32f 100644 --- a/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt @@ -38,6 +38,11 @@ Microsoft.Maui.Controls.ContentPresenter.Padding.set -> void Microsoft.Maui.Controls.ContentPresenter.UpdateChildrenLayout() -> void Microsoft.Maui.Controls.ContentView.SafeAreaEdges.get -> Microsoft.Maui.SafeAreaEdges Microsoft.Maui.Controls.ContentView.SafeAreaEdges.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -450,6 +455,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt index a765cb09c70d..3f8f462b71d4 100644 --- a/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt @@ -38,6 +38,11 @@ Microsoft.Maui.Controls.ContentPresenter.Padding.set -> void Microsoft.Maui.Controls.ContentPresenter.UpdateChildrenLayout() -> void Microsoft.Maui.Controls.ContentView.SafeAreaEdges.get -> Microsoft.Maui.SafeAreaEdges Microsoft.Maui.Controls.ContentView.SafeAreaEdges.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -430,6 +435,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt index a765cb09c70d..3f8f462b71d4 100644 --- a/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt @@ -38,6 +38,11 @@ Microsoft.Maui.Controls.ContentPresenter.Padding.set -> void Microsoft.Maui.Controls.ContentPresenter.UpdateChildrenLayout() -> void Microsoft.Maui.Controls.ContentView.SafeAreaEdges.get -> Microsoft.Maui.SafeAreaEdges Microsoft.Maui.Controls.ContentView.SafeAreaEdges.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -430,6 +435,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt index 120c935484bf..c772248acb9a 100644 --- a/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt @@ -7,6 +7,11 @@ const Microsoft.Maui.Controls.BrushTypeConverter.Rgb = "rgb" -> string! const Microsoft.Maui.Controls.BrushTypeConverter.Rgba = "rgba" -> string! Microsoft.Maui.Controls.BrushTypeConverter.GradientBrushParser.GradientBrushParser(Microsoft.Maui.Graphics.Converters.ColorTypeConverter? colorConverter = null) -> void Microsoft.Maui.Controls.BrushTypeConverter.GradientBrushParser.Parse(string? css) -> Microsoft.Maui.Controls.GradientBrush? +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -94,6 +99,8 @@ Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclarationAttribute.Al *REMOVED*~static Microsoft.Maui.Controls.MenuItem.GetAccelerator(Microsoft.Maui.Controls.BindableObject bindable) -> Microsoft.Maui.Controls.Accelerator *REMOVED*~static Microsoft.Maui.Controls.MenuItem.SetAccelerator(Microsoft.Maui.Controls.BindableObject bindable, Microsoft.Maui.Controls.Accelerator value) -> void *REMOVED*~static readonly Microsoft.Maui.Controls.MenuItem.AcceleratorProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty override Microsoft.Maui.Controls.BrushTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type! sourceType) -> bool override Microsoft.Maui.Controls.BrushTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool override Microsoft.Maui.Controls.BrushTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object! value) -> object? diff --git a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt index 94f0cd7ab6a1..c189c2ff9822 100644 --- a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt @@ -38,6 +38,11 @@ Microsoft.Maui.Controls.ContentPresenter.Padding.set -> void Microsoft.Maui.Controls.ContentPresenter.UpdateChildrenLayout() -> void Microsoft.Maui.Controls.ContentView.SafeAreaEdges.get -> Microsoft.Maui.SafeAreaEdges Microsoft.Maui.Controls.ContentView.SafeAreaEdges.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -441,6 +446,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt index c98a7b3105c1..a679a42998f4 100644 --- a/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt @@ -38,6 +38,11 @@ Microsoft.Maui.Controls.ContentPresenter.Padding.set -> void Microsoft.Maui.Controls.ContentPresenter.UpdateChildrenLayout() -> void Microsoft.Maui.Controls.ContentView.SafeAreaEdges.get -> Microsoft.Maui.SafeAreaEdges Microsoft.Maui.Controls.ContentView.SafeAreaEdges.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime oldDate, System.DateTime newDate) -> void Microsoft.Maui.Controls.DateChangedEventArgs.DateChangedEventArgs(System.DateTime? oldDate, System.DateTime? newDate) -> void *REMOVED*Microsoft.Maui.Controls.DateChangedEventArgs.NewDate.get -> System.DateTime @@ -412,6 +417,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt index c98a7b3105c1..8e600b8c8433 100644 --- a/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -30,6 +30,11 @@ Microsoft.Maui.Controls.ContentPage.SafeAreaEdges.get -> Microsoft.Maui.SafeArea Microsoft.Maui.Controls.ContentPage.SafeAreaEdges.set -> void Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparent.get -> bool Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparent.set -> void +override Microsoft.Maui.Controls.CheckBox.IsEnabledCore.get -> bool +~Microsoft.Maui.Controls.CheckBox.Command.get -> System.Windows.Input.ICommand +~Microsoft.Maui.Controls.CheckBox.Command.set -> void +~Microsoft.Maui.Controls.CheckBox.CommandParameter.get -> object +~Microsoft.Maui.Controls.CheckBox.CommandParameter.set -> void ~Microsoft.Maui.Controls.ContentPresenter.Children.get -> System.Collections.Generic.IReadOnlyList ~Microsoft.Maui.Controls.ContentPresenter.LowerChild(Microsoft.Maui.Controls.View view) -> void Microsoft.Maui.Controls.ContentPresenter.Padding.get -> Microsoft.Maui.Thickness @@ -412,6 +417,8 @@ static readonly Microsoft.Maui.Controls.Border.SafeAreaEdgesProperty -> Microsof *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.CommandProperty -> Microsoft.Maui.Controls.BindableProperty *REMOVED*~static readonly Microsoft.Maui.Controls.ClickGestureRecognizer.NumberOfClicksRequiredProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandParameterProperty -> Microsoft.Maui.Controls.BindableProperty +~static readonly Microsoft.Maui.Controls.CheckBox.CommandProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPage.SafeAreaEdgesProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.CascadeInputTransparentProperty -> Microsoft.Maui.Controls.BindableProperty ~static readonly Microsoft.Maui.Controls.ContentPresenter.PaddingProperty -> Microsoft.Maui.Controls.BindableProperty diff --git a/src/Controls/tests/Core.UnitTests/CheckBoxUnitTests.cs b/src/Controls/tests/Core.UnitTests/CheckBoxUnitTests.cs index faba353370e2..64e60bcac3e0 100644 --- a/src/Controls/tests/Core.UnitTests/CheckBoxUnitTests.cs +++ b/src/Controls/tests/Core.UnitTests/CheckBoxUnitTests.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; - using Xunit; using static Microsoft.Maui.Controls.Core.UnitTests.VisualStateTestHelpers; @@ -45,6 +41,49 @@ public void TestOnEventNotDoubleFired() Assert.False(fired); } + [Fact] + public void CheckedChangedEventArgs_ShouldHaveCorrectValue() + { + var checkBox = new CheckBox(); + CheckedChangedEventArgs eventArgs = null; + + checkBox.CheckedChanged += (sender, e) => eventArgs = e; + + // Test changing from false to true + checkBox.IsChecked = true; + Assert.NotNull(eventArgs); + Assert.True(eventArgs.Value); + + // Test changing from true to false + checkBox.IsChecked = false; + Assert.False(eventArgs.Value); + } + + [Fact] + public void CheckedChangedEvent_ShouldFireOnlyWhenValueChanges() + { + var checkBox = new CheckBox(); + int eventFireCount = 0; + + checkBox.CheckedChanged += (sender, e) => eventFireCount++; + + // Set to same value should not fire event + checkBox.IsChecked = false; + Assert.Equal(0, eventFireCount); + + // Change value should fire event + checkBox.IsChecked = true; + Assert.Equal(1, eventFireCount); + + // Set to same value again should not fire event + checkBox.IsChecked = true; + Assert.Equal(1, eventFireCount); + + // Change back should fire event + checkBox.IsChecked = false; + Assert.Equal(2, eventFireCount); + } + [Fact] public void CheckedVisualStates() { @@ -63,5 +102,127 @@ public void CheckedVisualStates() element.IsChecked = false; Assert.NotEqual(checkedStateName, stateGroup.CurrentState.Name); } + + [Fact] + public void CheckBoxClickWhenCommandCanExecuteFalse() + { + bool invoked = false; + var checkBox = new CheckBox() + { + Command = new Command(() => invoked = true, () => false), + IsChecked = false + }; + + checkBox.IsChecked = true; + + Assert.False(invoked); + } + + [Fact] + public void CheckBoxClickWhenCommandCanExecuteTrue() + { + bool invoked = false; + var checkBox = new CheckBox() + { + Command = new Command(() => invoked = true, () => true), + IsChecked = false + }; + + checkBox.IsChecked = true; + + Assert.True(invoked); + } + + [Fact] + public void Command_ShouldExecuteWithCorrectParameter() + { + object receivedParameter = null; + var expectedParameter = "TestParameter"; + + var checkBox = new CheckBox() + { + Command = new Command(param => receivedParameter = param, param => true), + CommandParameter = expectedParameter, + IsChecked = false + }; + + checkBox.IsChecked = true; + + Assert.Equal(expectedParameter, receivedParameter); + } + + [Fact] + public void Command_ShouldNotExecuteWhenNull() + { + var checkBox = new CheckBox() + { + Command = null, + IsChecked = false + }; + + // Should not throw exception when command is null + checkBox.IsChecked = true; + Assert.True(checkBox.IsChecked); + } + + [Fact] + public void CommandParameter_ShouldSupportNullValue() + { + object receivedParameter = "NotNull"; + + var checkBox = new CheckBox() + { + Command = new Command(param => receivedParameter = param, param => true), + CommandParameter = null, + IsChecked = false + }; + + checkBox.IsChecked = true; + + Assert.Null(receivedParameter); + } + + [Fact] + public void Command_ShouldExecuteOnlyOnCheckedStateChange() + { + int executeCount = 0; + var checkBox = new CheckBox() + { + Command = new Command(() => executeCount++, () => true), + IsChecked = false + }; + + // Change to true should execute command + checkBox.IsChecked = true; + Assert.Equal(1, executeCount); + + // Setting to same value should not execute command + checkBox.IsChecked = true; + Assert.Equal(1, executeCount); + + // Change to false should execute command + checkBox.IsChecked = false; + Assert.Equal(2, executeCount); + } + + [Fact] + public void Command_And_CheckedChanged_ShouldBothFire() + { + bool commandExecuted = false; + bool eventFired = false; + + var checkBox = new CheckBox() + { + Command = new Command(() => commandExecuted = true, () => true), + IsChecked = false + }; + + checkBox.CheckedChanged += (sender, e) => eventFired = true; + + checkBox.IsChecked = true; + + Assert.True(commandExecuted); + Assert.True(eventFired); + } } } diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_ChangeColor_VerifyVisualState.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_ChangeColor_VerifyVisualState.png index cd79afb1dbe4..1bc54f32b9bf 100644 Binary files a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_ChangeColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_ChangeColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_SetIsCheckedAndColor_VerifyVisualState.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_SetIsCheckedAndColor_VerifyVisualState.png index 9512f06aeff9..a59ab05ed3ac 100644 Binary files a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_SetIsCheckedAndColor_VerifyVisualState.png and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/CheckBox_SetIsCheckedAndColor_VerifyVisualState.png differ diff --git a/src/Controls/tests/TestCases.HostApp/FeatureMatrix/CheckBox/CheckBoxControlPage.xaml b/src/Controls/tests/TestCases.HostApp/FeatureMatrix/CheckBox/CheckBoxControlPage.xaml index 8238efa68edc..06d20ecc0e5f 100644 --- a/src/Controls/tests/TestCases.HostApp/FeatureMatrix/CheckBox/CheckBoxControlPage.xaml +++ b/src/Controls/tests/TestCases.HostApp/FeatureMatrix/CheckBox/CheckBoxControlPage.xaml @@ -7,18 +7,20 @@ Title="CheckBoxFeature"> @@ -57,10 +59,28 @@ AutomationId="CheckedChangedStatusLabel" Margin="0,0,10,10"/> + +