@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!
@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!
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 ?
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.
Answers
Switch should have a "IsToggled" boolean property… https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.switch?view=xamarin-forms
@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?
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/
I think the best way to do it would be create a property in your ViewModel like this
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 ?
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.
if you need to bind a command to an event you should use behaviours.
here is a wonderful lib
https://github.com/davidbritch/behaviors
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
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.