Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin forms: how to turn on all switches in a Listview?

SreeeeSreeee INMember ✭✭✭✭✭

I have a switch inside a Listview. I need to turn on all the switches when selecting an option from DisplayActionSheet. How can I do this feature?

Xaml Code:

<ListView 
    x:Name="UserList">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout
                        HorizontalOptions="FillAndExpand"
                        Orientation="Horizontal">
                        <Label 
                            Text="{Binding fullname}"
                            Font="11" 
                            TextColor="Black"
                            HorizontalOptions="Start" 
                            VerticalOptions="Center"/> 

                        <Switch 
                            HorizontalOptions="EndAndExpand"
                            VerticalOptions="CenterAndExpand"/>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Best Answer

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    The items in your list view should be bound to items in an observable list. Then each switch in the view cell is bound to a property on the item. Your code simply iterates through everything in your observable list, setting that property to true.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @JohnH Didn't get you. Can you show some code?

  • JohnHJohnH GBMember ✭✭✭✭✭

    @Sreeee said:
    @JohnH Didn't get you. Can you show some code?

    This is pretty basic Xamarin Forms and MVVM.
    In your example above, you already have a binding for the label Text in the viewcell view. You want another binding in the switch to a boolean property in the same item model.
    Then in your command or action, iterate through all the items and set that property to true.

Sign In or Register to comment.