---
title: "Bind an on/off switch in a C# desktop UI"
url: https://recursiveai.org/oneapp/cookbook/add-on-off-switch-to-desktop-ui/
description: "Bind OneApp Kit’s Switch to a C# boolean with Checked and OnCheckedChange. Run a desktop settings example that updates when you flip the switch."
version: "1.1.1"
---

This example opens a native window. For phone and tablet builds, see [OneApp’s mobile setup](https://recursiveai.org/oneapp/docs/mobile/).

`OneApp` opens the window; `OneApp.Kit` supplies `Switch`; `OneUI` compiles your markup.

```shell
dotnet new console --framework net10.0
dotnet add package RecursiveAI.OneApp.Kit
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```html
<Label For="sounds">Sound effects</Label>
<Switch Id="sounds" Checked="{SoundsOn}"
        OnCheckedChange="{SetSounds}" />
```

[The example](https://recursiveai.org/oneapp/cookbook/add-on-off-switch-to-desktop-ui/add-on-off-switch-to-desktop-ui.zip) is the full program below: unzip it and `dotnet run`.

Put this markup in `SettingsPage.html`. `SettingsPage.cs` defines both names: `SoundsOn` holds the on/off value and `SetSounds` updates it. The window starts at **Off**; flipping the switch updates both the switch and the status below it.

## The full program

The markup compiler generates the other half of `SettingsPage`, including the implementation of its `[State]` property.

```xml
<ItemGroup>
  <PackageReference
    Include="RecursiveAI.OneApp.Kit"
    Version="1.1.1" />
  <PackageReference
    Include="RecursiveAI.OneApp"
    Version="1.1.1" />
  <PackageReference
    Include="RecursiveAI.OneUI"
    Version="1.1.0" />
</ItemGroup>
```

`SettingsPage.html`:

```html
<Theme>
  <main style="padding: 24px">
    <div style="display: flex; gap: 8px;
                align-items: center">
      <Label For="sounds">Sound effects</Label>
      <Switch Id="sounds" Checked="{SoundsOn}"
              OnCheckedChange="{SetSounds}" />
    </div>
    <p id="status">
      {SoundsOn ? "On" : "Off"}
    </p>
  </main>
</Theme>
```

`SettingsPage.cs`:

```csharp
using OneUI.Components;

public partial class SettingsPage
{
    [State]
    private partial bool SoundsOn { get; set; }

    private void SetSounds(bool value)
    {
        SoundsOn = value;
        Console.WriteLine(
            SoundsOn ? "On" : "Off");
    }
}
```

`Program.cs` opens the window with the `SettingsPage` component defined above:

```csharp
using OneApp;

var options = new AppOptions(new SettingsPage())
{
    Title = "Settings",
    Width = 340,
    Height = 190,
};
using var host = AppHost.Create(options);
host.Run();
```

Click the switch, then press Space while it has focus. This prints the following (checked on Linux (X11)):

```text
On
Off
```

![The Settings window with the Sound effects switch and the status On](https://recursiveai.org/oneapp/images/add-on-off-switch-to-desktop-ui/switch-on.png)

If the switch snaps back or seems stuck, check that the callback assigns its argument to the same `[State]` property you pass to `Checked`.

## Variations

Use the small switch in a compact row. Keep `SoundsOn` and `SetSounds` from `SettingsPage.cs`:

```html
<Switch Id="sounds" Checked="{SoundsOn}"
        OnCheckedChange="{SetSounds}"
        Size="{OneApp.Kit.ControlSize.Sm}" />
```

To start on, add this method to `SettingsPage.cs`, which defines `SoundsOn`:

```csharp
protected override void OnMounted()
    => SoundsOn = true;
```

## Related recipes

- [Build a C# desktop app with HTML and CSS](https://recursiveai.org/oneapp/cookbook/build-desktop-app-with-html-css/)
- [Customize the desktop UI kit theme](https://recursiveai.org/oneapp/cookbook/theme-desktop-ui-kit/)

## The short answer

Pass a C# `bool` to `Switch.Checked` and a callback to `OnCheckedChange`. When the user flips the switch, accept the requested value by assigning it to your `[State]` property. The property is the source of truth for the switch and for anything else that depends on the setting. The sample keeps it in memory; it doesn’t play sound or save a preference.

## How do I make a button work as an on/off switch?

Use `Switch` instead of a text field or a button whose caption changes from “Enable” to “Disable.” OneApp Kit renders a button with `role="switch"` and reports the current value through `aria-checked`. Match `Label.For` to `Switch.Id` to give it a visible name. If there’s no visible label, set `AriaLabel`. Keep the name the same in both states: [the WAI-ARIA switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/) separates what the switch controls from whether it’s on.

With `OnCheckedChange` set, the switch asks the parent for the opposite value; it doesn’t commit the change itself. If the parent leaves `Checked` alone, the switch keeps showing that value. Without the callback the control toggles its own state, which is fine for a standalone switch but doesn’t update a setting that the rest of your app reads. Set `Disabled` when the setting isn’t available; a disabled switch doesn’t call the handler.

## When should I use a toggle switch instead of a checkbox?

Use a switch when a yes/no preference changes immediately, such as showing line numbers in an editor. If a user is filling out a form and still has to click **Apply**, a checkbox can make it clearer that the selection is pending. See the [toggle switch design guidelines](https://learn.microsoft.com/en-us/windows/apps/develop/ui/controls/toggles#choosing-between-toggle-switch-and-check-box). If the switch is also meant to survive a restart, load the saved boolean into the page’s state and save accepted changes from the handler. Merely binding `Checked` doesn’t persist anything.
