How to bind switch toggle event to Viewmodel in xamarin form.

BhautikBhautik Member ✭✭✭

Hi,
How to bind switch control toggle event to ViewModel in xamarin form.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
  • SzbestaSzbesta Member ✭✭✭

    @Bhautik I dont think thats possible as the toggled event is no bindable property. What you can do is create your own switch controll which has a Command that is executed after it is toggled, you can then bind a command from your ViewModel to the command!

  • BhautikBhautik Member ✭✭✭

    @Szbesta said:
    @Bhautik I dont think thats possible as the toggled event is no bindable property. What you can do is create your own switch controll which has a Command that is executed after it is toggled, you can then bind a command from your ViewModel to the command!

    Do you have any example?

  • SzbestaSzbesta Member ✭✭✭

    How to make a custom switch well....
    Just make a class inheriting from the switch class and give it a command property.
    But no i dont have a specific example, however, there are more than enough examples of custom controls if you google it.
    Tons of options:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view
    https://mindofai.github.io/Creating-Custom-Controls-with-Bindable-Properties-in-Xamarin.Forms/

  • seanydaseanyda GBMember ✭✭✭✭✭

    I think the best way to do it would be create a property in your ViewModel like this

    private bool _isSwitchToggled = false;
            public bool IsSwitchedToggled
            {
                get { return _isSwitchToggled; }
                set { 
                    _isSwitchToggled = value;
                    OnPropertyChanged(nameof(IsSwitchedToggled));
                }
            }
    

    and just bind that to the IsToggledProperty on the control. You can then have full use in your ViewModel whether it's toggled or not. Making a custom switch cell seems well unnecessary... unless im missing something ?

  • SzbestaSzbesta Member ✭✭✭

    I guess(ed) he wants to bind a command to the Toggled event as saig in his question might as well be wrong in my interpretation there.

  • BhautikBhautik Member ✭✭✭
    edited July 2018

    @Szbesta said:
    I guess(ed) he wants to bind a command to the Toggled event as saig in his question might as well be wrong in my interpretation there.

    Right, I want to bind a command to the toggled event and I have multiple switch and on particular switch toggle i want to refresh list in listview.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    if you need to bind a command to an event you should use behaviours.

    here is a wonderful lib

    https://github.com/davidbritch/behaviors

  • AdrianBowlesAdrianBowles USMember ✭✭
    edited November 2018

    Trying to get the behaviors to work with a switch and struggling and searching around it would appear others are too and looking for a simple solution. I simply want to bind the switch to trigger a command and can pass the toggled property as an event.


    <Switch.Behaviors>

    </Switch.Behaviors>

    and the code behind

        public ICommand ToggledCommand1 => new Command(async x => {
            await DisplayAlert("Toggled", "head", "OK");
        });
    

    but I get a compilation but the method is not invoked when I execute and toggle the switch. Any ideas on what is missing or a simple example would be great.

Sign In or Register to comment.