12
Vote

Accordion Throws Exceptions When Changing ItemsSource

description

When the accordion is set with a SelectionMode of "One" and the ItemsSource is changed there are two scenarios when a runtime exception is generated.
 
The first occurs when both the old and new values for the ItemsSource have at least one element in them. In this scenario UnselectItem will generate an RTE when calling ItemContainerGenerator.ContainerFromIndex(index).
 
The second occurs when the old value of ItemsSource has many elements and the new value has none. In this scenario UnselectItem will throw an RTE when attempting to access Items[0].
 
In the attached zip I have provided a small testing project which I used to reproduce these errors. I added the toolkit projects for local debugging. The first scenario can be reproduced using item source 1 then item source 2. The second scenario can be reproduced using item source 2 and then item source 3. The version of Accordion.cs alongside the project is one I have modified to no longer produce these errors. The modifications may or may not be entirely appropriate, but I think they will help illustrate some of the issues.

file attachments

comments

jayfreer wrote Nov 20, 2009 at 3:15 PM

I ran into the same issue. I have SelectionMode=ZeroOrMore. If no items are selected when the ItemsSource is switched, then I do not get an exception. I one or more items are selected, then I get the exception.

DanielAncines wrote Feb 9, 2010 at 11:40 PM

I have a similar problem, in my case the problem occurs when I clear the itemssource always the first accordionitem is selected.

zievw wrote Jul 19, 2012 at 1:04 PM

Please see attached behavior which solved this problem for me.

You use it by not defining ItemsSource on the Accordion itself but on the behavior instead:
<toolkit:Accordion>
    <i:Interaction.Behaviors>
        <behaviors:AccordionSafeItemsSourceBindingBehavior ItemsSource="{Binding MyList}"/>
    </i:Interaction.Behaviors>
</toolkit:Accordion>