How to select only one Toggle Switch in Listview?


I have a Listview which has toggle switches. I want to select only one item in Listview with toggle switch. When I select second toggle then first toggle must be de-active again. For example in the picture; When I select Rekorida and then I select Merchandizing , Rekorida must turn back disable. Every time only one option can be active. Is it possible to do it in Xamarin?

Example listview

My listView Code :

                    <ListView x:Name"ShipListView" HasUnevenRows"True" SeparatorVisibility"Default" SelectionMode"Single">

                                    <Grid Margin"4, 3, 4, 3" Padding"2" BackgroundColor"White">
                                        <Grid.RowDefinitions HeightRequest"40">
                                            <!--<RowDefinition Height"20"></RowDefinition>-->
                                            <ColumnDefinition Width"80*"></ColumnDefinition>
                                            <ColumnDefinition Width"*"></ColumnDefinition>
                                            <ColumnDefinition Width"40*"></ColumnDefinition>
                                        <Label Text"{Binding ShipName}" TextColor"DeepPink" Grid.Column"0" FontAttributes"Bold"/>
                                        <Switch  IsToggled"{Binding Selected}" Grid.Column"2" Toggled"OnShipToggled" />


And My function

async void OnShipToggled(object sender, ToggledEventArgs e)

        checkShipSelected  !checkShipSelected;

            ViewCell cell  (sender as Switch).Parent.Parent as ViewCell;

            ParametersMemberShipInformation model  cell.BindingContext as ParametersMemberShipInformation;

            if (model ! null)
                selectedMemberShipOid  model.Oid;


I’m trying get id of selected item in listview and I do this successfully. Bu I want users can only select one item at the same time because of nice visuality and not be confused.


Fetch your view model in your code behind file and then filter out the selected toggle and marked rest of them as false

private YourViewModel MyViewModel { get > BindingContext as YourViewModel; }

if (model ! null)
 selectedMemberShipOid  model.Oid;
 MyViewModel.ShipListView.Where(x> x.Oid ! 
     selectedMemberShipOid).Foreach(x> x.Selected  false)


Answered By – Hammad Shabbir

Leave a Comment