I am using an accordion in my silverlight app. In the accordionItem I am having a listbox and I need to access the expanded accordion item and this listbox of the accordion.
I am having hard time finding accessing the accordionitem and the listbox it is holding. How do I do this?
What I tried to solve it is to use the following extension class:
public static IEnumerable<DependencyObject> GetVisuals(this DependencyObject root)
{
int count = VisualTreeHelper.GetChildrenCount(root);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(root, i);
yield return child;
foreach (var descendants in child.GetVisuals())
{
yield return descendants;
}
}
}
and then call it for my accordion:
foreach (var control in MyAccordion.GetVisuals().OfType<ListBox>())
{
// Do something with the listbox
}
Unfortunately, even without the OfType extension method the Accordion reports to have 0 when GetChildrenCount is called inside the extension method.
UPDATE:
I found a way to access the AccordionItem using the following code
int selectedIndex = OrganiChartAccordion.SelectedIndex;
AccordionItem accordionItem = OrganiChartAccordion.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as AccordionItem;
In the AccordionItem I am using a DataTemplate to render the data. In the DataTemplate I have two listboxes. I need to access the data template in the accordionitem that is selected and inside the datatemplate the two listbox.