Reputation: 360
I have a ListView with Label and Checkbox
I want to implement a button lister that will get all checked items from my ListView
This is the ListView
<ListView ItemsSource="{Binding OCParticipantsTable}"
HasUnevenRows="True"
x:Name="dsfdf">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding FirstName_}"/>
<CheckBox/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is the ItemsSource property:
private ObservableCollection<ParticipantsTable> _OCParticipantsTable =
new ObservableCollection<ParticipantsTable>();
public ObservableCollection<ParticipantsTable> OCParticipantsTable
{
get { return _OCParticipantsTable; }
set
{
if (_OCParticipantsTable != value)
{
_OCParticipantsTable = value;
OnPropertyChanged("ListOfItems");
}
}
}
How can I implement a button lister that will get all checked items from my ListView ?
Something like that:
foreach (var pt in dsfdf.ItemsSource)
{
if (pt.CheckBox.IsChecked)
{
// do something...
}
}
Upvotes: 0
Views: 946
Reputation: 10346
As Jason said that you bind checkbox's IsChecked property to a property, then you can foreach the OCParticipantsTable to get all items that checkbox's IsChecked property is true.
<ListView
x:Name="dsfdf"
HasUnevenRows="True"
ItemsSource="{Binding OCParticipantsTable}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding name}" />
<CheckBox IsChecked="{Binding ischecked}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button
x:Name="btn1"
Clicked="Btn1_Clicked"
Text="getdata" />
This is the model, having some properties, please contain one bool property to binding CheckBox, and implementing INotifyPropertychanged interface, to notify data changed.
public class ParticipantsTable:ViewModelBase
{
public string name { get; set; }
private bool _ischecked;
public bool ischecked
{
get { return _ischecked; }
set
{
_ischecked = value;
RaisePropertyChanged("ischecked");
}
}
}
The ViewModelBase implement INotifyPropertyChanged:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public partial class Page15 : ContentPage
{
public ObservableCollection<ParticipantsTable> OCParticipantsTable { get; set; }
public Page15()
{
InitializeComponent();
OCParticipantsTable = new ObservableCollection<ParticipantsTable>()
{
new ParticipantsTable(){name="cherry",ischecked=false },
new ParticipantsTable(){name="barry",ischecked=true },
new ParticipantsTable(){name="annine",ischecked=false },
new ParticipantsTable(){name="wendy",ischecked=false },
new ParticipantsTable(){name="leo",ischecked=true },
new ParticipantsTable(){name="alex",ischecked=false }
};
this.BindingContext = this;
}
private void Btn1_Clicked(object sender, EventArgs e)
{
foreach(var pt in OCParticipantsTable)
{
if(pt.ischecked)
{
//do something you want to do
}
}
}
}
Upvotes: 2