# RadzenSwitch Class

A toggle switch component for boolean on/off states with a sliding animation. RadzenSwitch provides an alternative to checkboxes with a more modern toggle UI pattern, ideal for settings and preferences. Displays as a sliding toggle that users can click or drag to change between on (true) and off (false) states, providing visual feedback with a sliding animation and color change. Common uses include enabling/disabling settings or features, toggling visibility of sections, on/off preferences in configuration panels, and boolean options in forms. Supports keyboard navigation (Space/Enter to toggle) for accessibility. Unlike checkboxes, switches are typically used for immediate effects rather than form submission actions.

Namespace: [Radzen.Blazor](https://blazor.radzen.com/docs/api/Radzen.Blazor.md)

Assembly: Radzen.Blazor.dll

## Inheritance

- Object
- ComponentBase
- [RadzenComponent](https://blazor.radzen.com/docs/api/Radzen.RadzenComponent.md)
- [FormComponent<bool>](https://blazor.radzen.com/docs/api/Radzen.FormComponent-1.md)
- RadzenSwitch

## Implements

- IComponent
- IHandleEvent
- IHandleAfterRender
- [IRadzenFormComponent](https://blazor.radzen.com/docs/api/Radzen.IRadzenFormComponent.md)

## Syntax

```csharp
public class RadzenSwitch : FormComponent<bool>, IComponent, IHandleEvent, IHandleAfterRender, IRadzenFormComponent
```

## Examples

Basic switch:

```razor
<RadzenSwitch @bind-Value=@isEnabled />
```

Switch with label and change event:

```razor
<RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center">
    <RadzenSwitch @bind-Value=@notifications Change=@(enabled => Console.WriteLine($"Notifications: {enabled}")) />
    <RadzenLabel Text="Enable notifications" />
</RadzenStack>
```

Read-only switch for display:

```razor
<RadzenSwitch Value=@feature.IsEnabled ReadOnly="true" />
```

## Constructors

| Constructor | Description |
|-------------|-------------|
| `public RadzenSwitch()` | A toggle switch component for boolean on/off states with a sliding animation. RadzenSwitch provides an alternative to checkboxes with a more modern toggle UI pattern, ideal for settings and preferences. Displays as a sliding toggle that users can click or drag to change between on (true) and off (false) states, providing visual feedback with a sliding animation and color change. Common uses include enabling/disabling settings or features, toggling visibility of sections, on/off preferences in configuration panels, and boolean options in forms. Supports keyboard navigation (Space/Enter to toggle) for accessibility. Unlike checkboxes, switches are typically used for immediate effects rather than form submission actions. |

## Properties

| Property | Type | Description |
|------|------|-------------|
| InputAttributes | `IReadOnlyDictionary<string, object>` | Gets or sets additional HTML attributes to be applied to the underlying input element. This allows passing custom attributes like data-* attributes, aria-* attributes, or other HTML attributes directly to the input. |
| InputId | `string` | Gets the id of the input element: Name when set, otherwise an id derived from the component id. |
| LabelTargetId | `string` |  |
| ReadOnly | `bool` | Gets or sets whether the switch is read-only and cannot be toggled by user interaction. When true, the switch displays its current state but prevents clicking or keyboard toggling. Useful for displaying switch state in view-only forms or dashboards. |

## Methods

| Method | Returns | Description |
|------|---------|-------------|
| `BuildRenderTree(Rendering.RenderTreeBuilder __builder)` | `void` |  |
| `GetComponentCssClass()` | `string` |  |
| `Toggle()` | `Task` | Programmatically toggles the switch between on (true) and off (false) states. This method is public and can be called from code to toggle the switch without user interaction. Does nothing if the switch is disabled or read-only. Returns: A task representing the asynchronous toggle operation. |

## Inherited Members

- From [FormComponent<bool>](https://blazor.radzen.com/docs/api/Radzen.FormComponent-1.md): Change, CurrentPlaceholder, Disabled, Dispose, EditContext, FieldIdentifier, FocusAsync, Form, FormFieldContext, FormFieldLabelledBy, GetClassList, GetValue, HasValue, IsBound, LabelToLink, LinkLabelAsync, Name, NotifyFieldChanged, OnContextMenu, Placeholder, SetParametersAsync, ShouldLinkLabel, TabIndex, Value, ValueChanged, ValueExpression, _value
- From [RadzenComponent](https://blazor.radzen.com/docs/api/Radzen.RadzenComponent.md): AddContextMenu, Attributes, ContextMenu, Culture, CurrentStyle, Debounce, DefaultCulture, DefaultUICulture, Element, GetCssClass, GetId, GetIdAttribute, IsJSRuntimeAvailable, JSRuntime, Localize, MouseEnter, MouseLeave, OnAfterRenderAsync, OnBecameInvisible, OnInitialized, OnMouseEnter, OnMouseLeave, RaiseContextMenu, RaiseMouseEnter, RaiseMouseLeave, Reference, Style, UICulture, UniqueID, Visible
- From ComponentBase: Assets, AssignedRenderMode, DispatchExceptionAsync, InvokeAsync, OnAfterRender, OnInitializedAsync, OnParametersSet, OnParametersSetAsync, RendererInfo, ShouldRender, StateHasChanged

