Click here to Skip to main content
15,881,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a List<T> to which I am binding in a XAML front-end. The XAML uses a template for the items in the list. The names of the items in the list are on the left and on the right I have a couple of buttons.

When I delete an item from the list, the data binding quite correctly removes the name of the item from the list; however, I end up with an extra entry at the end of the list that has no name but just has the buttons.

I don't see a way round this because when I look at the Count property of the List<T>, it is not changed by removing an item. Even if I call TrimExcess(), it will not do anything because it has a threshold of 90% and the List<T> is more than 90% full.

I must be missing something here because this seems like a huge flaw in the data binding model.

Can anyone please suggest a way round this?

Kind wishes ~ Patrick

What I have tried:

The only way I can think of to fix this is to implement my own collection that ensures its Count always reflects exactly how many items are in the list.
Posted
Comments
Richard Deeming 1-Aug-16 11:06am    
The Count property on a List<T> instance always reflects the actual number of items in the list.

(Unless you're accessing the list from multiple threads without proper synchronization, and the internal data has been corrupted. But if that was the case, you'd be getting strange exceptions when you tried to access it.)

TrimExcess is about removing the excess capacity within the internal storage of the list. It doesn't affect the number of items in the list, or the value of the Count property.

There must be something else going on within your code.
Patrick Skelton 1-Aug-16 11:14am    
Thank you for the reply.

It must be strange indeed. I just put the following inside the class that contains the actual instance of the List<t></t>.

int a = _recentDevices.Entries.Count;
_recentDevices.RemoveAt( index );
int b = _recentDevices.Entries.Count;


And the Count remains unchanged. I am running debug code on an Android emulator. This is also the C# that comes with Xamarin. Wonder if any of these have anything to do with it? Sigh... I feel a whole bunch of time-consuming experiments coming on...
Patrick Skelton 1-Aug-16 11:25am    
Just spotted it. The class is a custom collection and it looks like it has not been fully implemented - i.e. it does not implement ICollection. My bad for not checking the type of it. Since it was using methods like Add() and RemoveAt(), I just assumed it was a List. :-|

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900