【问题标题】:Get count of checkbox checked items in listview- Xamarin.forms获取列表视图中复选框选中项目的计数 - Xamarin.forms
【发布时间】:2019-12-23 07:16:55
【问题描述】:

我有一个 xamarin.forms 应用程序,其中包含一个带有复选框的列表视图。单击按钮后,我可以获得列表视图的复选框选中项目。但是我想要实现的是,当用户单击每个列表项上的复选框时,顶部的标签将显示复选框单击计数。 例如; 20 个中有 11 个被选中。当用户选中或取消选中复选框时,如何实现此复选框选中的计数值?

我的数据模型

  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");
                }
            }
        }
    }

我的 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>

感谢任何帮助。

【问题讨论】:

  • 这里ItemsSource="{Binding} 的绑定属性是什么?可以用来计数。
  • @AndroDevil 查看我的答案,您可能会遇到当前的问题!!!!

标签: xamarin.forms


【解决方案1】:

另一个答案有漏洞:

  • 由于您要从 UI 中更改模型,因此始终建议您使用双向绑定,否则 UI 中的更改将永远不会反映在您的集合中。因此,您的复选框绑定如下所示

    IsChecked="{Binding Selected, Mode=TwoWay}" // this reflects the changes from View to VM/Model.
    
  • 一旦你这样做了,你就可以像下面这样检查计数:

    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
    

【讨论】:

    【解决方案2】:

    从列表的绑定源获取选中的列表项。即,如果项源是 TimeSheetList

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2016-08-04
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多