1

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.

2
  • What is the binding property here ItemsSource="{Binding} ? that you can use to get count. Commented Dec 23, 2019 at 7:43
  • @AndroDevil check out my answer you might face an issue with the current one!!!! Commented Dec 23, 2019 at 9:34

2 Answers 2

1

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
    
Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.