WPF 遍历控件listview中的checkbox控件

1、获取控件中子控件方法

/// 
        /// 获得指定元素的所有子元素(这里需要有一个从DataTemplate里获取控件的函数)
        /// 
        /// 
        /// 
        /// 
        public List GetChildObjects(DependencyObject obj, string name) where T : FrameworkElement
        {
            DependencyObject child = null;
            List childList = new List();
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);
                if (child is T && (((T)child).Name == name || string.IsNullOrEmpty(name)))
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects(child, ""));//指定集合的元素添加到List队尾
            }
            return childList;
        }

2、遍历listvew中chekbox调用方法

List cks = GetChildObjects(this.ListView_train, "checkBox");
            foreach (var item in cks)
            {
                item.Visibility = System.Windows.Visibility.Visible;
            }

 

你可能感兴趣的:(WPF,c#,WinFom)