|
| 1 | +--- |
| 2 | +title: Cancel Checkbox Change in .NET MAUI |
| 3 | +description: Learn how to cancel a Checkbox change in .NET MAUI using the IsCheckedChanged event. |
| 4 | +type: how-to |
| 5 | +page_title: Prevent Checkbox State Change in .NET MAUI |
| 6 | +meta_title: Prevent Checkbox State Change in .NET MAUI |
| 7 | +slug: cancel-checkbox-change-net-maui |
| 8 | +tags: checkbox, .net maui, ischeckedchanged, ischecked, binding |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 12.0.0 | Telerik UI for .NET MAUI CheckBox | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to cancel a state change in a Checkbox control in .NET MAUI based on a specific condition. This allows me to maintain control over the Checkbox's state and update it dynamically. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to prevent Checkbox state change in .NET MAUI? |
| 24 | +- How to handle the `IsCheckedChanged` event to cancel state change? |
| 25 | +- How to implement conditional logic for Checkbox state in .NET MAUI? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve this behavior, use the `IsCheckedChanged` event and set the `IsChecked` property conditionally. |
| 30 | + |
| 31 | +**1.** Create a `ViewModel` class to store and bind the Checkbox state. |
| 32 | + |
| 33 | +```csharp |
| 34 | +public class ViewModel : NotifyPropertyChangedBase |
| 35 | +{ |
| 36 | + private bool myproperty = false; |
| 37 | + public bool MyProperty |
| 38 | + { |
| 39 | + get => this.myproperty; |
| 40 | + set |
| 41 | + { |
| 42 | + if (this.myproperty != value) |
| 43 | + { |
| 44 | + this.myproperty = value; |
| 45 | + OnPropertyChanged(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +**2.** Use the `IsCheckedChanged` event to monitor state changes. |
| 53 | + |
| 54 | +**3.** Implement conditional logic to revert the state if a condition is not met. |
| 55 | + |
| 56 | +```csharp |
| 57 | +public partial class MainPage : ContentPage |
| 58 | +{ |
| 59 | + ViewModel vm; |
| 60 | + public bool OperationPassed = true; |
| 61 | + |
| 62 | + public MainPage() |
| 63 | + { |
| 64 | + InitializeComponent(); |
| 65 | + this.vm = new ViewModel(); |
| 66 | + this.BindingContext = this.vm; |
| 67 | + } |
| 68 | + |
| 69 | + private void RadCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e) |
| 70 | + { |
| 71 | + // Check condition and revert state |
| 72 | + if (this.OperationPassed is false) |
| 73 | + { |
| 74 | + this.vm.MyProperty = false; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + this.vm.MyProperty = true; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +**4.** The Checkbox definition in XAML: |
| 85 | + |
| 86 | +```xml |
| 87 | +<HorizontalStackLayout Spacing="5" VerticalOptions="Start"> |
| 88 | + <telerik:RadCheckBox IsChecked="{Binding MyProperty, Mode=TwoWay}" |
| 89 | + IsCheckedChanged="RadCheckBox_IsCheckedChanged" |
| 90 | + IsThreeState="False"/> |
| 91 | +</HorizontalStackLayout> |
| 92 | +``` |
| 93 | + |
| 94 | +## See Also |
| 95 | + |
| 96 | +- [CheckBox Overview](https://www.telerik.com/maui-ui/documentation/controls/checkbox/overview) |
0 commit comments