Skip to main content

LgCheckBox

LgCheckBox<TValue> binds a boolean value to a classic checkbox (CheckBoxKind.Check) or a toggle switch (CheckBoxKind.Toggle). The bound type must be bool or bool? (required for the indeterminate state).

<LgCheckBox Text="#ReceiveNotifications" @bind-Value="_model.NotificationsEnabled" />
  • Namespace: Lagoon.UI.Components
  • Inherits from: LgInputBase<TValue> → LgAriaComponentBase → LgComponentBase
  • Interfaces: ILgCheckBox<TValue>
  • NuGet package: Lagoon.UI

Usage​

Demonstration​

Try this component in the Lagoon showcase.

Simple checkbox​

Text is the text displayed next to the checkbox (TextPosition places it on the left or right); Label remains the standard form field label.

NotificationsPage.razor
<LgCheckBox Label="#Notifications"
Text="#ReceiveNotifications"
TextPosition="CheckBoxTextPosition.Right"
@bind-Value="_model.NotificationsEnabled" />
NotificationsPage.razor.cs
public partial class NotificationsPage : LgPage
{
private readonly NotificationsVm _model = new();

private sealed class NotificationsVm
{
public bool NotificationsEnabled { get; set; }
}
}

Toggle switch​

CheckBoxKind="CheckBoxKind.Toggle" renders the checkbox as a toggle switch.

AdvancedModePage.razor
<LgCheckBox Label="#AdvancedMode"
Text="#Enable"
CheckBoxKind="CheckBoxKind.Toggle"
TextPosition="CheckBoxTextPosition.Right"
@bind-Value="_model.AdvancedMode" />
AdvancedModePage.razor.cs
public partial class AdvancedModePage : LgPage
{
private readonly AdvancedVm _model = new();

private sealed class AdvancedVm
{
public bool AdvancedMode { get; set; }
}
}

Indeterminate state​

AllowIndeterminateState="true" allows the third state (neither checked nor unchecked); the bound property must then be bool?.

SelectionPage.razor
<LgCheckBox Text="#SelectAll"
AllowIndeterminateState="true"
@bind-Value="_model.SelectAll" />
SelectionPage.razor.cs
public partial class SelectionPage : LgPage
{
private readonly SelectionVm _model = new();

private sealed class SelectionVm
{
// bool? : null represents the indeterminate state (partial selection).
public bool? SelectAll { get; set; }
}
}

Cancel or confirm the toggle​

OnChangingAsync is called before the change and allows you to cancel it (args.Cancel); args.Item returns the object passed to the Item parameter, useful when the checkbox is repeated in a list.

FeatureListPage.razor
@foreach (FeatureVm feature in _features)
{
<LgCheckBox Text="@feature.Name"
Item="feature"
OnChangingAsync="OnFeatureChangingAsync"
@bind-Value="feature.IsEnabled" />
}
FeatureListPage.razor.cs
public partial class FeatureListPage : LgPage
{
// Features displayed: each checkbox carries its feature via Item.
private readonly List<FeatureVm> _features = new();

// Called before the toggle: prevents disabling a locked feature.
private Task OnFeatureChangingAsync(ChangingItemEventArgs<bool> args)
{
if (args.Item is FeatureVm feature && feature.IsLocked)
{
args.Cancel = true;
ShowWarning("#FeatureLocked");
}
return Task.CompletedTask;
}

private sealed class FeatureVm
{
public string Name { get; set; }
public bool IsEnabled { get; set; }
public bool IsLocked { get; set; }
}
}

Properties​

All [Parameter] properties of the component, including inherited ones.

NameTypeDefaultDeclared inDescription
AllowIndeterminateStateboolfalseLgCheckBoxAllows the indeterminate state (the bound property must be bool?).
CheckBoxKindCheckBoxKindCheckLgCheckBoxCheckbox rendering: classic checkbox or toggle switch.
ItemobjectnullLgCheckBoxData associated with the control, returned in ChangingItemEventArgs.Item.
OnChangingAsyncFunc<ChangingItemEventArgs<TValue>, Task>nullLgCheckBoxCallback called before value change; the change can be cancelled.
TextstringnullLgCheckBoxText displayed next to the checkbox.
TextPositionCheckBoxTextPositionLeftLgCheckBoxPosition of the text relative to the checkbox.
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgInputBaseAdditional HTML attributes applied to the rendered element.
AutoFocusboolfalseLgInputBaseGives focus to the control on first render.
ConfirmationMessagestringnullLgInputBaseConfirmation message displayed before applying the toggle.
ContainerIInputContainer<TValue>nullLgInputBaseInternal use: attaches a GridView cell to the control.
CssClassstringnullLgInputBaseAdditional CSS classes.
DisabledboolfalseLgInputBaseDisables the control.
DisplayNamestringnullLgInputBaseField name used in error messages.
HideValidationMessageboolfalseLgInputBaseHides the validation message below the control.
LabelstringnullLgInputBaseField label (standard label area for form fields).
LabelContentRenderFragmentnullLgInputBaseCustom content for the label area.
OnChangeEventCallback<ChangeEventArgs>—LgInputBaseCallback called after value change.
OnResetEventCallback<ChangeEventArgs>—LgInputBaseCallback called on control reset.
PolicyEditstringnullLgInputBasePolicy required to modify the control.
PolicyVisiblestringnullLgInputBasePolicy required to display the control.
ReadOnlyboolfalseLgInputBaseDisplays the control in read-only state.
Requiredbool?nullLgInputBaseRequired field. If null, inferred from the [Required] attribute of the bound property.
ResetButtonbool?nullLgInputBaseDisplays the clear button. If null, the application behavior configuration applies.
ResetTextstringnullLgInputBaseClear button text.
ResetTextAriaLabelstringnullLgInputBaseAccessible label (aria-label) for the clear button.
TooltipstringnullLgInputBaseTooltip.
TooltipIsHtmlboolfalseLgInputBaseIndicates whether Tooltip content is in HTML format (otherwise plain text).
ValueTValue—LgInputBaseBound value (@bind-Value), of type bool or bool?.
ValueChangedEventCallback<TValue>—LgInputBaseCallback for updating the bound value.
ValueExpressionExpression<Func<TValue>>—LgInputBaseExpression identifying the bound property.
AriaLabelstringnullLgAriaComponentBaseValue of the aria-label attribute.
AriaLabelledBystringnullLgAriaComponentBaseValue of the aria-labelledby attribute.
Cascading parameters

LgCheckBox also consumes parameters automatically provided by ancestor components (you don't need to define them): ParentAuthorizeView (LgAuthorizeView) and the editing context of a parent LgEditForm.

Events​

EventTypeDescription
OnChangingAsyncFunc<ChangingItemEventArgs<TValue>, Task>Before the toggle; args.Cancel = true cancels it, args.Item returns the associated data.
OnChangeEventCallback<ChangeEventArgs>After the toggle.
OnResetEventCallback<ChangeEventArgs>When the control is reset.

Associated enumerations​

CheckBoxKind​

Control rendering (CheckBoxKind property).

ValueDescription
CheckClassic checkbox (default value).
ToggleToggle switch.

CheckBoxTextPosition​

Position of Text relative to the checkbox.

ValueDescription
LeftText on the left (default value).
RightText on the right.

Best practices / accessibility​

  • Use bool? with AllowIndeterminateState only: on a simple bool, the indeterminate state has no representation.
  • Provide Text (or AriaLabel): a checkbox without text has no accessible label.
  • For an immediate on/off action, see also LgToggle.
Complete reference

All parameters (including inherited ones): API Reference — LgCheckBox.