Custom Switch in Xamarin Forms

Xamarin Forms is a wonderful ecosystem to develop cross platform mobile applications.

Like any platforms, it has limitations too, but yeah there are work arounds. At times you simply can contribute to the awesome “Open Source Xamarin Forms”

Today we will be discussing a small problem I faced in one of our projects, where Xamarin Forms Switch control fires “Toggled” event both when changed by user or programmatically. This was not ideal for our requirements.

So I planned to create a Custom Switch over the Original Switch.

Warning: This work around might not be the best way to do this.

To get started create a Class File, in my case “CustomSwitch”, inherit it from Switch class of Xamarin Forms.

We will create a IsCustomToggled property (which is Bindable Property), to use instead of IsToggled and CustomToggled instead of Toggled event.

Once we reach the constructor, we subscribe to the original Toggled event with our own function.

Also whenever IsCustomToggled is changed in code / program, we will Unsubscribe before setting original IsToggled, and then subscribe back to the event.

The CustomToggled event has CustomToggledEventArgs which contain the new value for switch and an bool “isUser” for indicating whether the action was result of user interaction or code change.


Once the Custom Switch is ready, the usage at any place in project is straight forward,

from Xaml


Samples and CustomSwitch available at GitHub

Hope this helps someone with same issue, in case of any queries do drop a mail to muhaymin at fantacode com where I am available Xamarin Consulting  or use the comment section below.



One thought on “Custom Switch in Xamarin Forms”

  1. Made a small change to the bindable property.

    public static readonly BindableProperty IsCustomToggledProperty = BindableProperty.Create(
    propertyChanging: (bindable, oldValue, newValue) =>
    if (oldValue != newValue)
    var control = bindable as CustomSwitch;
    control.IsCustomToggled = (bool)newValue;

    I’m no Xamarin genius by any means but I never got the binding to actually work unless implementing the propertyChanging method.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.