Anand
Anand

Reputation: 1959

Get count of checkbox checked items in listview- Xamarin.forms

I have a xamarin.forms app which contains a list view with checkbox.I can get the checkbox checked items of list view after a button click. But what I am trying to achieve is when user click on checkbox on each list item, a label on top will show the checkbox clicked count. For eg; 11 out of 20 selected. How can I implement this checkbox selected count value when user check or uncheck checkbox?

My data model

  public class TimeSheetListData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public string EmployeeID { get; set; }         
        public string EndDate { get; set; }
        public string SlNo { get; set; }


    //Checkbox selection
        private bool selected;
        public bool Selected
        {

            get
            {
                return selected;
            }

            set
            {
                if (value != null)
                {
                    selected = value;
                    NotifyPropertyChanged("Selected");
                }
            }
        }
    }

My xaml

    <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"> 
     // Where Iam trying to show count
 <Label x:Name="Count" Text="" FontSize="Small" TextColor="Black" VerticalOptions="Center" >
     <ListView  x:Name="TimesheetListView"  ItemsSource="{Binding} "                                                                   
                  HeightRequest="{Binding Path=Height, Source={x:Reference ListLayout}}"                      
                  BackgroundColor="Transparent" 
                  CachingStrategy="RecycleElement"
                  SeparatorVisibility="None"
                  HasUnevenRows="True"                       
                  HorizontalOptions="FillAndExpand"        
                  Margin="11,2,11,2"            
                  VerticalOptions="FillAndExpand">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>                                
                                     <Frame ClassId="{Binding EmployeeID}"  BorderColor="LightGray" >      
                                    <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical">                               
                                     <Label Text="{Binding EndDate}" FontSize="Small" TextColor="Black" VerticalOptions="Center" >
                                                    </Label>
                                     <Label Text="{Binding SlNo}" FontSize="Small" TextColor="Black" VerticalOptions="Center" >
                                      </Label>
                                     <CheckBox x:Name="MultiSelectCheckBox" Grid.Column="2" Color="#004d6f" IsChecked="{Binding Selected}"  IsVisible="{Binding IsCheckBoxVisible}"  HorizontalOptions="End" VerticalOptions="Center"></CheckBox>
                                     </StackLayout>                                                
                                     </Frame>             
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView          
        </StackLayout>

Any help is appreciated.

Upvotes: 1

Views: 827

Answers (2)

FreakyAli
FreakyAli

Reputation: 16449

The other answer has a loophole:

  • Since you are making a change in the model from the UI it is always recommended that you use Two-way binding otherwise the changes from UI will never be reflected in your collection. So your Checkbox Binding would look something like below

    IsChecked="{Binding Selected, Mode=TwoWay}" // this reflects the changes from View to VM/Model.
    
  • Once you do that you can just check the count like follows:

    var count = TimeSheetList.Count(t => t.Selected); //In case your Collection is a List
    
    int count = TimeSheetList.Where(p => p.IsActiveUserControlChecked).Count; //If its an observable collection
    

Upvotes: 1

Mukesh Modhvadiya
Mukesh Modhvadiya

Reputation: 2178

Get checked list items from binding source of list.. i.e. if item source is TimeSheetList

var totalSelectedItems = TimeSheetList.Where(t => t.Selected== true).Count();

Upvotes: 1

Related Questions