Problem with accordion in silverlight
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 o开发者_StackOverflowr know the reason why I get the exception and can help. Thanks
This works fine for me using "Lore Ipsum.." text, the toolkit Accordion and a bunch of AccordionItems - the problem must be in some of your code that you are not showing here. Can you post a stacktrace and the XAML?
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:
if (i == ivDialogList.Count - 1)
ai.IsSelected = true;
part in the loop out, so it would be set after the loop is done, like this:
((AccordionItem)content.Items[ivDialogList.Count - 1]).IsSelected = true;
And that made it work like a charm.
精彩评论