Wp8:Not able to get checkBox in listbox

做~自己de王妃 提交于 2019-12-06 05:08:49
Kulasangar

Are you asking about getting the Checked property of the Checkbox?

Is this the one you were looking for?. Sample code to find the Children control within a Parent using VisualTreeHelper:

 private ChildControl FindVisualChild<ChildControl>(DependencyObject DependencyObj)
    where ChildControl : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(DependencyObj); i++)
        {
            DependencyObject Child = VisualTreeHelper.GetChild(DependencyObj, i);

            if (Child != null && Child is ChildControl)
            {
                return (ChildControl)Child;
            }
            else
            {
                ChildControl ChildOfChild = FindVisualChild<ChildControl>(Child);

                if (ChildOfChild != null)
                {
                    return ChildOfChild;
                }
            }
        }
        return null;
    }
user3207655

Hi got the solution here. there is no need to set virtualization property its simple.

private void GetItemsRecursive(DependencyObject lb)
{
    var childrenCount = VisualTreeHelper.GetChildrenCount(lb);

    for (int i = 0; i < childrenCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(lb, i);

        if (child is CheckBox) // specific/child control 
        {
            CheckBox targeted_element = (CheckBox)child;

            targeted_element.IsChecked = true;

            if (targeted_element.IsChecked == true)
            {

                return;
            }
        }

        GetItemsRecursive(child);
    }
}

just a bit change at DependencyObject child = VisualTreeHelper.GetChild(lb, i); instead of var child

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