There is a listbox. At a certain point, a piece of code is executed that increments SelectedIndex by one. Naturally, this possibility is first checked by comparing with Items.Count . And then the fun begins. An exception is thrown.

The specified argument is out of range.

Begin to understand. I look in the debugging control values

enter image description here

I go further and try to look at the collection set and see this

enter image description here

Explain to me how it is? Count == 2, but there is no access to the item with index 1. Who faced?

  • The number may be equal to 2, but not the fact that the other elements are initialized. I can be wrong, but if you create a List <T> for 10 elements and refer to an element that is not initialized, you will get the same error. - iluxa1810
  • @ iluxa1810, no, if you turn to an uninitialized element, you will get null. - iRumba
  • @iRumba, and how is the collection filled? - skubarenko
  • one
    @iRumba: And give a reproducing example. Maybe you do it in some event handler, who knows. - VladD
  • @VladD, not quite in the handler ... I do it on command. In fact, I had an idea why this happened, but there is no time to check. The following happens. Listbox is bound to CollectionView with filter from the main collection. When the command is executed, 2 actions occur in different events. 1 - the item of the collection changes the property by which the view is filtered and the item from the collection disappears. 2 - the index shifts by 1. But Count, apparently, does not have time to react to the first action (which is strange, this is obviously a bug). - iRumba

0