问题
I am new to C#/XAML development and I have just started creating very basic windows phone 8 application. I want to get all the elements (Controls) added in the stackpanel as child to the stackpanel using stackpanel.Children.Add(name_of_the_control). Now I want to get all the elements or controls added in the stackpanel but do not know how to do it. I have searched on the net but could not get helpful link.
Please help me with the process of how to achieve it.
回答1:
You can access all controls added to StackPanel from it's Children
property, for example :
foreach(var control in stackpanel.Children)
{
//here you can get each element of stackpanel in control variable
//further example :
if(control is RadioButton)
{
var radio = (RadioButton)control;
//do something with RadioButton
}
else if(control is TextBlock)
{
var textblok = (TextBlock)control;
//do something with TextBlock
}
//add other possible type of control you want to manipulate
}
来源:https://stackoverflow.com/questions/22298431/traversing-elements-of-stackpanel