获取listview-Xamarin.forms中复选框选中项的计数
我有一个xamarin.forms应用程序,其中包含一个带有复选框的列表视图。单击按钮后,我可以获取列表视图中的复选框选中项。但我试图实现的是,当用户单击每个列表项上的复选框时,顶部的标签将显示已单击的复选框计数用于eg;已选中20个复选框中的11个。当用户选中或取消选中复选框时,如何实现此复选框选中的计数值 我的数据模型获取listview-Xamarin.forms中复选框选中项的计数,xamarin.forms,Xamarin.forms,我有一个xamarin.forms应用程序,其中包含一个带有复选框的列表视图。单击按钮后,我可以获取列表视图中的复选框选中项。但我试图实现的是,当用户单击每个列表项上的复选框时,顶部的标签将显示已单击的复选框计数用于eg;已选中20个复选框中的11个。当用户选中或取消选中复选框时,如何实现此复选框选中的计数值 我的数据模型 public class TimeSheetListData : INotifyPropertyChanged { public event Pr
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");
}
}
}
}
<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>
我的xaml
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");
}
}
}
}
<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>
//我想让你知道我在哪里
从列表的绑定源获取选中的列表项。。i、 e.如果项目来源是时间表列表
var totalSelectedItems = TimeSheetList.Where(t => t.Selected== true).Count();
另一个答案有一个漏洞:
- 由于您要从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
这里的binding属性是什么ItemsSource=“{binding}
?您可以使用它来获取计数。@AndroDevil请查看我的答案您可能会遇到当前的问题!!!!