Forum Xamarin Xamarin.Forms

How to check a checkbox when a item in a listview is selected

Hello everyone my name is Taniguchi and i am very new at xamarin forms, i would like to know how can i check the checkbox when a item in a listview is selected and how can i multiselect items in a listview.

Answers

  • GraverobberGraverobber Member ✭✭✭

    Here are my ideas without code, I hope it is okay^^

    For multi selection it might be easier to use a CollectionView instead of a ListView.
    Then set the SelectionMode of the CollectionView to Multiple.

    Are you following MVVM?
    If yes, then your ItemModel should have a bool IsSelected which is Bound to the checked state of the checkbox.

    Then add a SelectionChanged event to your CollectionView and when it is fired all the selected items will be in the CurrentSelection list of your SelectionChangedEventArgs.
    You might need to iterate over your CollectionViews Source in order to check if this element is part of the currentSelection. If it is you set its IsSelected attribute to true, otherwise to false.

  • DeepakDYDeepakDY INMember ✭✭✭
    edited July 2019

    Hello @TaniguchiSantos ,

    Firstly Bind Checkbox IsCheck propery

    <CheckBox IsChecked="{Binding IsCheck}"/>

    Then create ListView ItemSelected event

         private void ItemList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
                {
                    var item = e.SelectedItem as Model;
                    item.IsCheck = true;
                }
    
  • GraverobberGraverobber Member ✭✭✭

    Yes @DeepakDY is right, you don't need a multiple selection feature, one by one is okay, so you can stay with ListView.
    One small addition I would set item.IsCheck like this

    item.IsCheck = !item.IsCheck;

    That way it gets selected/deselected depending on its current state

  • TaniguchiSantosTaniguchiSantos Member ✭✭✭

    @Graverobber , @DeepakDY i need to multiselect items because i am working on an appointment app, i need to select multiple rows to change the status of row to sent for sending to a manager to approve

  • JarvanJarvan Member, Xamarin Team Xamurai

    i need to multiselect items ...

    CollectionView supports multiple select, set SelectionMode to Multiple. CollectionView provides a more flexible, and performant alternative to ListView.

    See the link:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/selection#single-selection
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/introduction

Sign In or Register to comment.