I have a listview and all the listview item contain a switch at the right end like below picture.
When I select an item the switch fires the Toggled event. My codes are adding below:
Xaml:
<Switch IsToggled="false" Margin="210,2,2,2" Toggled="Switch_Toggled" />
Xaml.cs:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
// I need the retail_modified_item_id of the selected item, how I can access that
}
private void accept(object sender, EventArgs args)
{
// fetch all selected items and display success
DisplayAlert("Success", "Request Accepted and Updated", "OK");
}
Model class:
namespace XamNative.Models
{
public class Human
{
public string name { get; set; }
public int retail_modified_item_id { get; set; }
public double old_price { get; set; }
public double new_price { get; set; }
}
}
I need the retail_modified_item_id of all the selected item[item1,item2],when i click accept button ?
get the BindingContext of the switch, and from that get the ID you need
// list to hold all selected values
List<string> selected = new List<string>();
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
// I need the retail_modified_item_id of the selected item, how I can access that
var switch = (Switch)sender;
var human = (Human)switch.BindingContext;
var id = human.retail_modified_item_id;
// add/remove id from selected based on IsToggled
if (switch.IsToggled) {
if (!selected.Contains(id)) selected.Add(id);
} else {
if (selected.Contains(id)) selected.Remove(id);
}
}