-
Notifications
You must be signed in to change notification settings - Fork 19
Added new kb article cancel-checkbox-change-net-maui #1296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
github-actions
wants to merge
1
commit into
master
Choose a base branch
from
new-kb-cancel-checkbox-change-net-maui-e95ac3843594436b800e30909a6dac85
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| title: Cancel Checkbox Change in .NET MAUI | ||
| description: Learn how to cancel a Checkbox change in .NET MAUI using the IsCheckedChanged event. | ||
| type: how-to | ||
| page_title: Prevent Checkbox State Change in .NET MAUI | ||
| meta_title: Prevent Checkbox State Change in .NET MAUI | ||
| slug: cancel-checkbox-change-net-maui | ||
| tags: checkbox, .net maui, ischeckedchanged, ischecked, binding | ||
| res_type: kb | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| | Version | Product | Author | | ||
| | --- | --- | ---- | | ||
| | 12.0.0 | Telerik UI for .NET MAUI CheckBox | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| This knowledge base article also answers the following questions: | ||
| - How to prevent Checkbox state change in .NET MAUI? | ||
| - How to handle the `IsCheckedChanged` event to cancel state change? | ||
| - How to implement conditional logic for Checkbox state in .NET MAUI? | ||
|
|
||
| ## Solution | ||
|
|
||
| To achieve this behavior, use the `IsCheckedChanged` event and set the `IsChecked` property conditionally. | ||
|
|
||
| **1.** Create a `ViewModel` class to store and bind the Checkbox state. | ||
|
|
||
| ```csharp | ||
| public class ViewModel : NotifyPropertyChangedBase | ||
| { | ||
| private bool myproperty = false; | ||
| public bool MyProperty | ||
| { | ||
| get => this.myproperty; | ||
| set | ||
| { | ||
| if (this.myproperty != value) | ||
| { | ||
| this.myproperty = value; | ||
| OnPropertyChanged(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **2.** Use the `IsCheckedChanged` event to monitor state changes. | ||
|
|
||
| **3.** Implement conditional logic to revert the state if a condition is not met. | ||
|
|
||
| ```csharp | ||
| public partial class MainPage : ContentPage | ||
| { | ||
| ViewModel vm; | ||
| public bool OperationPassed = true; | ||
|
|
||
| public MainPage() | ||
| { | ||
| InitializeComponent(); | ||
| this.vm = new ViewModel(); | ||
| this.BindingContext = this.vm; | ||
| } | ||
|
|
||
| private void RadCheckBox_IsCheckedChanged(object sender, IsCheckedChangedEventArgs e) | ||
| { | ||
| // Check condition and revert state | ||
| if (this.OperationPassed is false) | ||
| { | ||
| this.vm.MyProperty = false; | ||
| } | ||
| else | ||
| { | ||
| this.vm.MyProperty = true; | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **4.** The Checkbox definition in XAML: | ||
|
|
||
| ```xml | ||
| <HorizontalStackLayout Spacing="5" VerticalOptions="Start"> | ||
| <telerik:RadCheckBox IsChecked="{Binding MyProperty, Mode=TwoWay}" | ||
| IsCheckedChanged="RadCheckBox_IsCheckedChanged" | ||
| IsThreeState="False"/> | ||
| </HorizontalStackLayout> | ||
| ``` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [CheckBox Overview](https://www.telerik.com/maui-ui/documentation/controls/checkbox/overview) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check if there are other possible resources to link to (a checkbox API, articles about selection, etc.) |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a step. Should be a complete sentence, if it is.