CheckBox
The CheckBox control is a toggle button that allows users to select one or more options from a set. It can be in one of two states: checked or unchecked.
Overview
The WinUI CheckBox provides a familiar and accessible way for users to make binary selections. It's a fundamental control for forms, settings, and any scenario where a true/false or on/off choice is needed.
Key Features
- Visual States: Supports standard visual states like Normal, PointerOver, Pressed, and Disabled.
- Checked/Unchecked States: Clearly indicates its current selection state.
- Accessibility: Built with accessibility in mind, supporting keyboard navigation and screen readers.
- Customization: Can be styled and templated to match your application's design.
Usage Examples
Basic CheckBox
A simple CheckBox with a label.
XAML (Conceptual):
<CheckBox Content="Accept Terms and Conditions" />
C# (Conceptual):
var acceptTermsCheckbox = new CheckBox { Content = "Accept Terms and Conditions" };
Disabled CheckBox
A CheckBox in a disabled state.
XAML (Conceptual):
<CheckBox Content="Option is Disabled" IsEnabled="False" />
C# (Conceptual):
var disabledCheckbox = new CheckBox { Content = "Option is Disabled", IsEnabled = false };
Custom Styling CheckBox
Demonstrates a more custom CheckBox appearance.
Enable Notifications
Receive Newsletter
HTML/CSS for Custom CheckBox:
<div class="custom-checkbox-container">
<input type="checkbox" id="customCheck1">
<span class="custom-checkbox"></span>
Enable Notifications
</div>
<div class="custom-checkbox-container">
<input type="checkbox" id="customCheck2" checked>
<span class="custom-checkbox"></span>
Receive Newsletter
</div>
CSS:
.custom-checkbox-container {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 1rem;
font-size: 1rem;
cursor: pointer;
user-select: none;
}
.custom-checkbox {
width: 22px;
height: 22px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
display: inline-block;
position: relative;
transition: background-color 0.2s, border-color 0.2s;
}
.custom-checkbox-container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
.custom-checkbox:after {
content: "";
position: absolute;
display: none;
left: 7px;
top: 3px;
width: 5px;
height: 10px;
border: solid #fff;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
}
.custom-checkbox-container input:checked ~ .custom-checkbox {
background-color: #0062CC; /* WinUI Accent */
border-color: #0062CC;
}
.custom-checkbox-container input:checked ~ .custom-checkbox:after {
display: block;
}
.custom-checkbox-container input:focus ~ .custom-checkbox {
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}
Properties
Common properties for the CheckBox control include:
Content: The text or UI element displayed next to the CheckBox.IsChecked: A boolean property indicating whether the CheckBox is currently checked. Can betrue,false, ornull(for indeterminate state).IsEnabled: Controls whether the control is interactive.Visibility: Controls whether the control is visible.
Events
The primary event for a CheckBox is:
Checked: Fired when the CheckBox becomes checked.Unchecked: Fired when the CheckBox becomes unchecked.Indeterminate: Fired when the CheckBox enters the indeterminate state.
Best Practices
- Use CheckBoxes for independent choices where selecting one option doesn't affect the selection of others.
- For mutually exclusive options, consider using RadioButtons.
- Ensure labels are clear and concise.
- Handle the
IsCheckedproperty changes appropriately in your application logic. - Always provide visual feedback for user interactions.