Skip to main content

Toggle Binders

Binders for the Unity UI Toggle component.


ToggleIsOnBinder

Binds the Toggle.isOn state.

InterfaceDescription
IBinder<bool>Sets isOn from the ViewModel
IReverseBinder<bool>Sends changes back

Inspector properties

PropertyDescription
_converterOptional value converter; the reverse direction works through ITwoWayConverter

Loop protection

A write from the ViewModel raises onValueChanged for the other listeners, but the binder does not send it back to the ViewModel.

Modes: OneWay, TwoWay, OneTime, OneWayToSource.

[ViewModel]
public partial class SettingsViewModel
{
[TwoWayBind] private bool _musicEnabled;
[TwoWayBind] private bool _soundEnabled;
}

ToggleCommandBinder

Binds a command to Toggle.onValueChanged.

InterfaceDescription
IBinder<IRelayCommand>Calls Execute() on toggle
IBinder<IRelayCommand<bool>>Calls Execute(isOn) with the current state

InteractableMode

The reaction to CanExecute, as in ButtonCommandBinder:

ModeBehaviour
Interactabletoggle.interactable = canExecute
VisiblegameObject.SetActive(canExecute)
NoneIgnores it
CustomCalls ICanExecuteHandler.SetCanExecute(bool)

Parameterized variants

BinderCommandExtra parameters
ToggleCommandBinderIRelayCommand / IRelayCommand<bool>
ToggleCommandBinder<T>IRelayCommand<bool, T>1 parameter
ToggleCommandBinder<T1, T2>IRelayCommand<bool, T1, T2>2 parameters
ToggleCommandBinder<T1, T2, T3>IRelayCommand<bool, T1, T2, T3>3 parameters

The first command parameter is always the current isOn state.

Modes: OneWay, OneTime.

[ViewModel]
public partial class SettingsViewModel
{
[RelayCommand]
private void ToggleMusic(bool isOn) { /* ... */ }
// → IRelayCommand<bool> ToggleMusicCommand
}

ToggleIsOnEnumBinder / ToggleIsOnEnumGroupBinder

Set isOn from an enum value through SetIsOnWithoutNotify: the Enum variant for a single Toggle, EnumGroup for a set of Toggles where every enum member maps to its own element.

Modes: OneWay, OneTime.


ToggleGroupAllowSwitchOffBinder

Binds ToggleGroup.allowSwitchOff. Turning it off selects nothing: an empty group stays empty until the user clicks.

Modes: OneWay, OneTime.


Example: inverted settings

[ViewModel]
public partial class NotificationViewModel
{
[TwoWayBind] private bool _doNotDisturb;
}

In the Inspector set the BoolInvertConverter on ToggleIsOnBinder, so the Toggle shows "Notifications on" (!doNotDisturb).


See also