Problem adding and deleting items to the PathListBox's source

Aug 11, 2010 at 6:38 PM
Edited Aug 11, 2010 at 6:39 PM
I'm having an intermittent issue with adding and removing items from the source object that the PathListBox is bound to. Upon deleting an item the PathListBox updates visually, but the StartItemIndex of one of its items is then equal to its count.

If the PathListBoxItem which has the high StartItemIndex set = to its count is clicked next an ArgumentOutOfRangeException is thrown on line 221 of PathListBoxScrollBehavior.cs. There's an if statement testing whether or not the StartItemIndex >= the Items.Count. Normally them being equal would be an issue since the count is 0 based. However, in this situation, something is getting updated between the time the item is clicked on, and when it scrolls. This update also means that the next time the PathListBox is clicked on, the StartItemIndex for each item has been reset to its correct value, and the error doesn't occur again.

When I have time (next week) I'll dig into the lower elements of the PathListBox's template and bind a content presenter to the StartItemIndex to find out what's getting updated when. For now, I've made the following change from:

if (pathListBox.StartItemIndex >= pathListBox.Items.Count)

to

if (pathListBox.StartItemIndex > pathListBox.Items.Count)

rebuilt, and it functions correctly; that is that no errors are thrown and the PathListBox scrolls as expected.

It turns out that this is simply a symptom of a deeper issue. If you change the underlying source to which the PathListBox is bound to a source with fewer items, the StartItemIndex does not update, and the program will crash. The StartItemIndex for each item needs to be updated when the source is updated, not at some later time. I'm not sure if this is an issue with the PathListBoxUtils library or the PathListBox.