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.
Answer: 1
Try this:
<UserControl x:Class="SilverlightApplication10.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:layout="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <layout:Accordion x:Name="Accordion1"> <layout:Accordion.Items> <layout:AccordionItem Header="Header 1"> <ListBox> <ListBox.Items> <ListBoxItem Content="Item 1"></ListBoxItem> <ListBoxItem Content="Item 2"></ListBoxItem> <ListBoxItem Content="Item 3"></ListBoxItem> </ListBox.Items> </ListBox> </layout:AccordionItem> <layout:AccordionItem Header="Header 2"> <ListBox> <ListBox.Items> <ListBoxItem Content="Item 1"></ListBoxItem> <ListBoxItem Content="Item 2"></ListBoxItem> <ListBoxItem Content="Item 3"></ListBoxItem> </ListBox.Items> </ListBox> </layout:AccordionItem> </layout:Accordion.Items> </layout:Accordion> </Grid> </UserControl>
And the code-behind:
using System; using System.Diagnostics; using System.Linq; using System.Windows; using System.Windows.Controls; namespace SilverlightApplication10 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += MainPage_Loaded; } void MainPage_Loaded(object sender, RoutedEventArgs e) { Accordion1.Items.ToList().ForEach(item => { ((item as AccordionItem).Content as ListBox).Items.ToList().ForEach(listBoxItem => { var content = ((ListBoxItem)listBoxItem).Content; Debug.WriteLine(content); }); }); } } }
by : Henrik Söderlundhttp://stackoverflow.com/users/250605
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog