且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在按钮单击事件中调用stackpanel子项

更新时间:2023-10-29 16:29:04

Hi Pavan



您可以使用此功能在StackPanel中找到子项

http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname%28v=vs .110%29.aspx [ ^ ]



Hi Pavan

You can find child from StackPanel with this function which is given here
http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname%28v=vs.110%29.aspx[^]

private childItem FindVisualChild<childitem>(DependencyObject obj)
    where childItem : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is childItem)
            return (childItem)child;
        else
        {
            childItem childOfChild = FindVisualChild<childitem>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}





并按下按钮点击代码 -





and call following code on your button click -

object obj = FindVisualChild<mycan>(stkm);





如果要在stakpanel中添加多个子项,则为每个子项提供Name,并在FindVisualChild函数中使用Name属性进行检查。



If you will add more than one child items into the stakpanel then give "Name" to each child item and check inside "FindVisualChild" function with "Name" property.