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

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:

Events

The primary event for a CheckBox is:

Best Practices