Reputation: 2224
I have two custom listView such as ListView A and ListView B. On ListView A contains the list of items when I click item in ListView A it will be highlighted(ie check and uncheck) on the specific item/data is populating into ListView B it is working fine. But when I uncheck item from ListView A and similar way trying to remove that item from ListView B it is not working.
While removing an item from custom ListView with check box not removing an item from ListView. but I am able to add that item but not able to remove.
onitemtap event of ListView control on the specific condition I am trying to Add or Remove the item from a list.
public class TestIds
{
ObservableCollection<ListViewModel> ListItems { get; set; } = new ObservableCollection<ListViewModel>()
{
};
private void ListView_ItemTappedEventArgs(ItemTappedEventArgs itemTapped)
{
items = itemTapped.Item as ListViewModel;
if (items.IsSelected)
{
items.IsSelected = false;
BindRemoveItem(items.ID, items.IsSelected);
}
else
{
items.IsSelected = true;
BindRemoveItem(items.ID, items.IsSelected);
}
}
//this is my code for binding and removing data from listView
private void BindRemoveItem(string id, bool isChecked)
{
IDManager.GetIDList(list =>
{
foreach (IDEntities item in list)
{
if (isChecked == true)
{
ListItems.Add(new ListViewModel { Text = item.IDDescription + " (" + item.IDCodes + ")", ID = item.IDCodeID });
}
else
{
ListItems.Remove(new ListViewModel { Text = item.IDDescription + " (" + item.IDCodes + ")", ID = item.IDCodeID };
}
}
CustomControlClass.ListView.ItemsSource = ListItems;
}, id);
}
}
Here ListItems.Remove
I am not able to item from listview.
Upvotes: 0
Views: 120
Reputation: 311
I think I've found the issue, although I am not sure If I understood your question correctly.
You are trying to remove a new object here
ListItems.Remove(new ListViewModel { Text = item.IDDescription + " (" + item.IDCodes + ")", ID = item.IDCodeID };
The comparison is being done by reference, and nothing is being removed in the end.
Instead you shoud do
ListItems.Remove(ListItems.FirstOrDefault(i => i.ID == item.IDCodeID))
That is assuming IDCodeID is a unique identifier of your object, if not then you should probably consider some other way of getting the reference to the object that you want to remove
Upvotes: 2