I want to get all control's id of silverlight page. So I want to iterate through all controls inside xaml page. For this I have used following code:
private List<UIElement> GetElement(UIElement parent, Type targetType)
{
List<UIElement> items = new List<UIElement>();
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (child.GetType() == targetType)
{
items.Add(child);
}
}
}
return items;
}
and I call above function on my button click event as below.
List<UIElement> lsttexts = GetElement(LayoutRoot, typeof(System.Windows.Controls.SLFramework.UniTextBox));
List<UIElement> lstlabels = GetElement(LayoutRoot, typeof(TextBlock));
List<UIElement> lstbtn = GetElement(LayoutRoot, typeof(Button));
List<UIElement> lstcombo = GetElement(LayoutRoot, typeof(ComboBox));
List<UIElement> lstStackpanel = GetElement(LayoutRoot, typeof(StackPanel));
Demo Example of my page:
<TextBlock Text="Name : " Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<StackPanel x:Name="stkpnl" Grid.Row="0" Grid.Column="1" Orientation="Vertical">
<StackPanel x:Name="childpanel1" Orientation="Horizontal">
<TextBlock x:Name="lbl1" Text="Name : " HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox x:Name="txt1" Text="=test1"></TextBox>
</StackPanel>
<StackPanel x:Name="childpanel11" Orientation="Horizontal">
<TextBlock x:Name="lbl11" Text="Name : " HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox x:Name="txt11" Text="=test11"></TextBox>
</StackPanel>
</StackPanel>
It get all textbol and textbox outside the satckpanel, listitem and tab control.
But I want to get all control of page includiing which are inside TabControl, statckpanel and Listitem.
Thanks
ItemCollection Items
property of the TabControl.