Recursive AI

Add checkable items to a C# dropdown menu

OneApp is a free, open-source framework for apps in .NET whose UI is HTML and CSS, drawn natively with no web view on Windows, macOS, Linux, iOS and Android, and run in the browser, from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Build a View menu with two on/off settings, Grid and Rulers, both off at startup.

Terminal
dotnet new console -o Example
cd Example
dotnet add package RecursiveAI.OneApp
dotnet add package RecursiveAI.OneApp.Kit

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

Menu and Grid come from Demo.cs, shown below.

HTML
<DropdownMenu Model="{Menu}">
  <template slot="trigger">
    <Button>View</Button>
  </template>
  <DropdownMenuCheckboxItem
    Model="{Menu}" Checked="{Grid}"
    OnCheckedChange="{v => Grid = v}"
    CloseOnSelect="{false}">Grid
  </DropdownMenuCheckboxItem>
</DropdownMenu>

The example is the full program below: unzip it and dotnet run.

Put the snippet in Demo.html. The download adds Rulers as a second independent toggle.

Open View, toggle Grid and Rulers, then press Escape to see the settings shown in the window:

Text
Grid: true; Rulers: true

Full program

Checked on Linux (X11) with OneApp and OneApp Kit 1.1.1.

The zip pins both packages in Example.csproj. OneUI, OneApp’s UI layer, comes with RecursiveAI.OneApp; no separate install is needed. Its source generator implements the [State] properties in Demo.cs. It pairs Demo.html with the partial class Demo by matching the template’s base name to the class name.

XML
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference
      Include="RecursiveAI.OneApp"
      Version="1.1.1" />
    <PackageReference
      Include="RecursiveAI.OneApp.Kit"
      Version="1.1.1" />
  </ItemGroup>
</Project>

Demo.html:

HTML
<Theme>
  <main style="padding: 32px">
    <DropdownMenu Id="view" Model="{Menu}">
      <template slot="trigger">
        <Button Id="show" AriaHasPopup="menu"
          AriaControls="view"
          AriaExpanded="{Expanded}">
          View
        </Button>
      </template>
      <DropdownMenuCheckboxItem
        Model="{Menu}"
        TextValue="Grid" Checked="{Grid}"
        OnCheckedChange="{v => Grid = v}"
        CloseOnSelect="{false}">Grid
      </DropdownMenuCheckboxItem>
      <DropdownMenuCheckboxItem
        Model="{Menu}"
        TextValue="Rulers" Checked="{Rulers}"
        OnCheckedChange="{v => Rulers = v}"
        CloseOnSelect="{false}">Rulers
      </DropdownMenuCheckboxItem>
    </DropdownMenu>
    <p id="settings"
      >Grid: {Grid}; Rulers: {Rulers}</p>
  </main>
</Theme>

Demo.cs:

C#
using OneApp.Kit;
using OneUI.Components;
 
public partial class Demo
{
    public MenuModel Menu { get; } = new();
 
    [State]
    public partial bool Grid { get; set; }
    [State]
    public partial bool Rulers { get; set; }
    [State]
    private partial string Expanded { get; set; }
 
    public Demo()
    {
        Expanded = "false";
        Menu.Changed += () =>
            Expanded = Menu.Open ? "true" : "false";
    }
}

Program.cs opens the window:

C#
using OneApp;
 
var options = new AppOptions(new Demo())
{
    Title = "View options",
    Width = 480,
    Height = 340,
};
using var host = AppHost.Create(options);
host.Run();

View menu with Grid checked and Rulers unchecked

If a checkmark doesn’t change, make sure OnCheckedChange writes to the property you pass to Checked. Each item must also share the parent’s MenuModel.

Variations

Close the menu after a toggle. CloseOnSelect defaults to true, so remove the CloseOnSelect attribute from the Grid item:

HTML
<DropdownMenuCheckboxItem Model="{Menu}"
  TextValue="Grid" Checked="{Grid}"
  OnCheckedChange="{v => Grid = v}">
  Grid
</DropdownMenuCheckboxItem>

The short answer

Use one DropdownMenuCheckboxItem per independent setting. Give the dropdown and every item the same MenuModel, but bind each item’s Checked and OnCheckedChange to its own C# Boolean. Set CloseOnSelect to false if the user should be able to toggle more than one item before dismissing the menu.

How do I use checkboxes in a C# dropdown menu?

The checkbox item is an action with an on or off state; it doesn’t select a single value for the whole dropdown. Clicking Grid requests the inverse of Grid. The C# callback accepts that value by storing it, and the next render sends the value back through Checked. Rulers has a separate property and can be on at the same time. The sample keeps both settings in memory, so reopening the menu preserves the checks, while restarting the app resets them.

The shared MenuModel tracks the menu itself, not which preferences are on. The sample listens to its Changed event and copies the open flag into a [State] property. That keeps AriaExpanded on the View button current. The kit renders the items with role="menuitemcheckbox" and exposes each setting as aria-checked; the mark is not the only indication of the state.

Should I use a checkable menu or a single-selection dropdown?

Use a checkable menu for independent options such as Grid and Rulers. Use a single-selection control when the values replace one another, such as a choice of units. OneApp’s SelectBox holds one value; it isn’t a collection of independent switches. If you need a menu of mutually exclusive choices, the kit also provides DropdownMenuRadioItem, with checked state supplied by your app.

In a checkbox menu, Arrow keys move among items; Space or Enter toggles the focused entry. Escape closes the menu and returns focus to the trigger. TextValue lets the kit jump to an item when the user types the beginning of its label. Keep it in sync with the visible label so typing R takes the user to Rulers. The component kit reference lists the other menu keys.

All oneapp recipes