menu

MAUI

  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Class SfSwitch - MAUI API Reference | Syncfusion

    Show / Hide Table of Contents

    Class SfSwitch

    Represents the SfSwitch class.

    Inheritance
    System.Object
    SfView
    SfSwitch
    Implements
    IDrawableLayout
    Microsoft.Maui.Graphics.IDrawable
    Microsoft.Maui.IAbsoluteLayout
    Microsoft.Maui.ILayout
    Microsoft.Maui.IView
    Microsoft.Maui.IElement
    Microsoft.Maui.ITransform
    Microsoft.Maui.IContainer
    System.Collections.Generic.IList<Microsoft.Maui.IView>
    System.Collections.Generic.ICollection<Microsoft.Maui.IView>
    System.Collections.Generic.IEnumerable<Microsoft.Maui.IView>
    System.Collections.IEnumerable
    Microsoft.Maui.ISafeAreaView
    Microsoft.Maui.IPadding
    Microsoft.Maui.ICrossPlatformLayout
    Microsoft.Maui.IVisualTreeElement
    Inherited Members
    SfView.ArrangeContent(Rect)
    SfView.ArrangeOverride(Rect)
    SfView.Children
    SfView.ClipToBounds
    SfView.IDrawableLayout.DrawingOrder
    SfView.IDrawableLayout.InvalidateDrawable()
    SfView.MeasureOverride(Double, Double)
    SfView.Padding
    Namespace: Syncfusion.Maui.Buttons
    Assembly: Syncfusion.Maui.Buttons.dll
    Syntax
    public class SfSwitch : SfView, IDrawableLayout, IDrawable, IAbsoluteLayout, ILayout, IView, IElement, ITransform, IContainer, IList<IView>, ICollection<IView>, IEnumerable<IView>, IEnumerable, ISafeAreaView, IPadding, ICrossPlatformLayout, IVisualTreeElement, ISemanticsProvider, IKeyboardListener, ITouchListener, ITapGestureListener, IGestureListener, IParentThemeElement, IThemeElement
    Examples

    The below example shows how to initialize the switch.

    • XAML
    • C#
    <syncfusion:SfSwitch/>
    SfSwitch sfSwitch = new SfSwitch();
    this.Content = sfSwitch;

    Constructors

    SfSwitch()

    Initializes a new instance of the SfSwitch class.

    Declaration
    public SfSwitch()

    Fields

    AllowIndeterminateStateProperty

    Identifies the AllowIndeterminateState bindable property.

    Declaration
    public static readonly BindableProperty AllowIndeterminateStateProperty
    Field Value
    Type Description
    Microsoft.Maui.Controls.BindableProperty

    The identifier for the AllowIndeterminateState bindable property.

    IsEnabledProperty

    Identifies the IsEnabled bindable property.

    Declaration
    public static readonly BindableProperty IsEnabledProperty
    Field Value
    Type Description
    Microsoft.Maui.Controls.BindableProperty

    The identifier for the IsEnabled bindable property.

    IsOnProperty

    Identifies the IsOn bindable property.

    Declaration
    public static readonly BindableProperty IsOnProperty
    Field Value
    Type Description
    Microsoft.Maui.Controls.BindableProperty

    The identifier for the IsOn bindable property.

    SwitchSettingsProperty

    Identifies the SwitchSettings bindable property.This property contains all the properties of SfSwitch control.

    Declaration
    public static readonly BindableProperty SwitchSettingsProperty
    Field Value
    Type Description
    Microsoft.Maui.Controls.BindableProperty

    The identifier for the SwitchSettings bindable property.

    Properties

    AllowIndeterminateState

    Gets or sets a value indicating whether to allow indeterminate state or not. When sfSwitch AllowIndeterminateState as false, it moves IsOn to true when it is null.

    Declaration
    public bool AllowIndeterminateState { get; set; }
    Property Value
    Type
    System.Boolean
    Examples

    The below example shows how to AllowIndeterminateState value in switch.

    • XAML
    • C#
    <syncfusion:SfSwitch AllowIndeterminateState="False"/>
    SfSwitch sfSwitch = new SfSwitch();
    sfSwitch.AllowIndeterminateState = False;
    this.Content = sfSwitch;

    IsEnabled

    Gets or sets a value indicating whether users can interact with the control.

    Declaration
    public bool IsEnabled { get; set; }
    Property Value
    Type
    System.Boolean
    Examples

    The below example shows how to set IsEnabled value in switch.

    • XAML
    • C#
    <syncfusion:SfSwitch IsEnabled="True"/>
    SfSwitch sfSwitch = new SfSwitch();
    sfSwitch.IsEnabled = true;
    this.Content = sfSwitch;

    IsOn

    Gets or set a value indicating whether the switch is in the "On" or "Off" state.

    Declaration
    public Nullable<bool> IsOn { get; set; }
    Property Value
    Type
    System.Nullable<System.Boolean>
    Remarks

    If the AllowIndeterminateState is true, the switch state will be null.

    Examples

    The below example shows how to set IsOn value in switch.

    • XAML
    • C#
    <syncfusion:SfSwitch IsOn="True"/>
    SfSwitch sfSwitch = new SfSwitch();
    sfSwitch.IsOn = true;
    this.Content = sfSwitch;

    SwitchSettings

    Gets or Sets the value of SwitchSettings.

    Declaration
    public SwitchSettings SwitchSettings { get; set; }
    Property Value
    Type
    SwitchSettings
    Examples

    The below example shows how to set SwitchSettings values in switch

    • XAML
    • C#
    <syncfusion:SfSwitch>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup  x:Name="CommonStates">
                <VisualState x:Name="On">
                    <VisualState.Setters>
                        <Setter Property="SwitchSettings">
                            <Setter.Value>
                                <syncfusion:SwitchSettings
                                    ThumbStrokeThickness="2"
                                    TrackStrokeThickness="2"
                                    TrackHeightRequest="30" 
                                    TrackWidthRequest="50" 
                                    TrackCornerRadius="22"
                                    ThumbCornerRadius="20" 
                                    ThumbHeightRequest="22" 
                                    ThumbWidthRequest="22"
                                    ThumbBackground="#FFFFFF"  
                                    TrackBackground="#6750A4"  
                                    ThumbStroke="#FFFFFF"
                                    TrackStroke="#6750A4"
                                    IconColor="#21005E"
                                    CustomPath = "M14.2051 0.744141L15.207 1.74609L5.69727 11.2559L0.792969 6.35156L1.79492 5.34961L5.69727 9.25195L14.2051 0.744141Z" />
                            </Setter.Value>
                        </Setter>  
                    </VisualState.Setters>
                </VisualState>  
                <VisualState x:Name="Off">
                    <VisualState.Setters>
                        <Setter Property="SwitchSettings">
                            <Setter.Value>
                                <syncfusion:SwitchSettings
                                    ThumbStrokeThickness="2"
                                    TrackStrokeThickness="2"
                                    TrackHeightRequest="30" 
                                    TrackWidthRequest="50" 
                                    TrackCornerRadius="20"
                                    ThumbCornerRadius="20" 
                                    ThumbHeightRequest="14" 
                                    ThumbWidthRequest="14"
                                    ThumbBackground="#79747E"  
                                    TrackBackground="#22029BFF"                                
                                    TrackStroke="#79747E"
                                    ThumbStroke="#79747E"/>
                            </Setter.Value>
                        </Setter>  
                    </VisualState.Setters>
                </VisualState> 
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </syncfusion:SfSwitch>    
    SfSwitch sfSwitch = new SfSwitch();
    SwitchSettings onStyle = new SwitchSettings();
    onStyle.TrackBackground = new SolidColorBrush(Color.FromRgba("6750A4"));
    onStyle.ThumbBackground = new SolidColorBrush(Color.FromRgba("FFFFFF"));
    onStyle.TrackStroke = Color.FromRgba("6750A4");
    onStyle.ThumbStroke = Color.FromRgba("FFFFFF");
    onStyle.TrackWidthRequest = 50;
    onStyle.TrackHeightRequest = 30;
    onStyle.ThumbWidthRequest = 22;
    onStyle.ThumbHeightRequest = 22;
    onStyle.TrackStrokeThickness = 2;
    onStyle.ThumbStrokeThickness = 2;
    onStyle.TrackCornerRadius = 20;
    onStyle.ThumbCornerRadius = 20;
    onStyle.IconColor = Color.FromRgba("21005E");
    onStyle.CustomPath = "M14.2051 0.744141L15.207 1.74609L5.69727 11.2559L0.792969 6.35156L1.79492 5.34961L5.69727 9.25195L14.2051 0.744141Z";
    
    SwitchSettings offStyle = new SwitchSettings();
    offStyle.TrackBackground = new SolidColorBrush(Color.FromRgba("22029BFF"));
    offStyle.ThumbBackground = new SolidColorBrush(Color.FromRgba("79747E"));
    offStyle.TrackStroke = Color.FromRgba("79747E");
    offStyle.ThumbStroke = Color.FromRgba("79747E");
    offStyle.TrackWidthRequest = 50;
    offStyle.TrackHeightRequest = 30;
    offStyle.ThumbWidthRequest = 14;
    offStyle.ThumbHeightRequest = 14;
    offStyle.TrackStrokeThickness = 2;
    offStyle.ThumbStrokeThickness = 2;
    offStyle.TrackCornerRadius = 20;
    offStyle.ThumbCornerRadius = 20;
    
    VisualStateGroupList visualStateGroupList = new VisualStateGroupList();
    VisualStateGroup commonStateGroup = new VisualStateGroup();
    
    VisualState onState = new VisualState { Name = "On" };
    onState.Setters.Add(new Setter { Property = SfSwitch.SwitchSettingsProperty, Value= onStyle});
    
    VisualState offState = new VisualState { Name = "Off" };
    offState.Setters.Add(new Setter { Property = SfSwitch.SwitchSettingsProperty, Value= offStyle});
    
    commonStateGroup.States.Add(onState);
    commonStateGroup.States.Add(offState);
    
    visualStateGroupList.Add(commonStateGroup);
    VisualStateManager.SetVisualStateGroups(sfSwitch, visualStateGroupList);
    this.Content = sfSwitch;

    Methods

    ChangeVisualState()

    Declaration
    protected override void ChangeVisualState()

    Finalize()

    Disconnect constructor

    Declaration
    protected override void Finalize()

    GetSemanticsNodesCore(Double, Double)

    Returns the semantics node list

    Declaration
    protected override List<SemanticsNode> GetSemanticsNodesCore(double width, double height)
    Parameters
    Type Name Description
    System.Double width
    System.Double height
    Returns
    Type
    System.Collections.Generic.List<Syncfusion.Maui.Graphics.Internals.SemanticsNode>
    Overrides
    SfView.GetSemanticsNodesCore(Double, Double)

    MeasureContent(Double, Double)

    Declaration
    protected override Size MeasureContent(double widthConstraint, double heightConstraint)
    Parameters
    Type Name Description
    System.Double widthConstraint
    System.Double heightConstraint
    Returns
    Type
    Microsoft.Maui.Graphics.Size
    Overrides
    SfView.MeasureContent(Double, Double)

    OnBindingContextChanged()

    Call when the binding context is changed.

    Declaration
    protected override void OnBindingContextChanged()
    Overrides
    SfView.OnBindingContextChanged()

    OnDraw(ICanvas, RectF)

    Declaration
    protected override void OnDraw(ICanvas canvas, RectF dirtyRect)
    Parameters
    Type Name Description
    Microsoft.Maui.Graphics.ICanvas canvas
    Microsoft.Maui.Graphics.RectF dirtyRect
    Overrides
    SfView.OnDraw(ICanvas, RectF)

    OnHandlerChanged()

    Declaration
    protected override void OnHandlerChanged()
    Overrides
    SfView.OnHandlerChanged()

    OnPropertyChanged(String)

    Declaration
    protected override void OnPropertyChanged(string propertyName = null)
    Parameters
    Type Name Description
    System.String propertyName

    OnSizeAllocated(Double, Double)

    Declaration
    protected override void OnSizeAllocated(double width, double height)
    Parameters
    Type Name Description
    System.Double width
    System.Double height

    Events

    StateChanged

    Invoke the event when the state of SfSwitch is changed.

    Declaration
    public event EventHandler<SwitchStateChangedEventArgs> StateChanged
    Event Type
    Type
    System.EventHandler<SwitchStateChangedEventArgs>

    StateChanging

    Occurs when the state of the control is about to change.

    Declaration
    public event EventHandler<SwitchStateChangingEventArgs> StateChanging
    Event Type
    Type
    System.EventHandler<SwitchStateChangingEventArgs>
    Remarks

    The StateChanging event is raised when the state of the control is about to change. It provides an opportunity to perform additional actions or validation before the state changes.

    Implements

    IDrawableLayout
    Microsoft.Maui.Graphics.IDrawable
    Microsoft.Maui.IAbsoluteLayout
    Microsoft.Maui.ILayout
    Microsoft.Maui.IView
    Microsoft.Maui.IElement
    Microsoft.Maui.ITransform
    Microsoft.Maui.IContainer
    System.Collections.Generic.IList<>
    System.Collections.Generic.ICollection<>
    System.Collections.Generic.IEnumerable<>
    System.Collections.IEnumerable
    Microsoft.Maui.ISafeAreaView
    Microsoft.Maui.IPadding
    Microsoft.Maui.ICrossPlatformLayout
    Microsoft.Maui.IVisualTreeElement
    Back to top Generated by DocFX
    Copyright © 2001 - 2025 Syncfusion Inc. All Rights Reserved