Traversing elements of Stackpanel

别等时光非礼了梦想. 提交于 2019-12-11 13:21:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!