I have a list of strings that I loop throu, and then add them in a Accordion. When I’ve added all of them I want the last item to expand. The code looks like this:
for (int i = 0; i < ivDialogList.Count; i++)
{
AccordionItem ai = new AccordionItem();
ai.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
ai.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Stretch;
ai.Content = ivDialogList[i].Message;
ai.Header = ivDialogList[i].PostType + " " + ivDialogList[i].User + " " + ivDialogList[i].PostDate;
if (i == ivDialogList.Count - 1)
ai.IsSelected = true;
content.Items.Add(ai);
}
This is working fine, but as soon as I click on any of the other accordion items or close the last one, I get an out of range exception. Does any body have another way of doing this or know the reason why I get the exception and can help.
Thanks
I managed to solve the question. I think the problem was somewhere in the loop, and probably specific for my code. What I did was that I moved the:
part in the loop out, so it would be set after the loop is done, like this:
And that made it work like a charm.