wpf 寻找某个控件下的子控件

/// <summary>
        /// 寻找某个控件下的子控件
        /// </summary>
        /// <typeparam name="ChildType"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj,i);
                if (child!=null&& child is ChildType)
                {
                    return child as ChildType;
                }
                else
                {
                    ChildType childOfChildren = FindVisualChild<ChildType>(child);
                    if (childOfChildren!=null)
                    {
                        return childOfChildren;
                    }
                }
            }
            return null;
            
        }
posted @ 2016-10-24 17:10  pengde  阅读(2916)  评论(0编辑  收藏  举报