Ejemplo n.º 1
0
        /// <summary>
        /// 深层递归遍历,查找特定类型、特定名称的子对象
        /// </summary>
        public static T GetChildObject <T>(DependencyObject objReference, string strName) where T : FrameworkElement
        {
            T objRetval = null;

            do
            {
                if (null == objReference)
                {
                    break;
                }

                if (String.IsNullOrEmpty(strName))
                {
                    objRetval = VisualTreeHelperEx.GetChildObject <T>(objReference);
                    break;
                }

                int iChildrenCount = VisualTreeHelper.GetChildrenCount(objReference);
                if (0 >= iChildrenCount)
                {
                    break;
                }

                for (int i = 0; i < iChildrenCount; ++i)
                {
                    DependencyObject objChild = VisualTreeHelper.GetChild(objReference, i);
                    if (objChild is T && ((T)objChild).Name == strName)
                    {
                        objRetval = (T)objChild;
                        break;
                    }

                    T objGrandChild = VisualTreeHelperEx.GetChildObject <T>(objChild, strName);
                    if (null != objGrandChild)
                    {
                        objRetval = objGrandChild;
                        break;
                    }
                }
            }while (false);

            return(objRetval);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 深层递归遍历,查找特定类型的子对象
        /// </summary>
        public static T GetChildObject <T>(DependencyObject objReference) where T : DependencyObject
        {
            T objRetval = null;

            do
            {
                if (null == objReference)
                {
                    break;
                }

                int iChildrenCount = VisualTreeHelper.GetChildrenCount(objReference);
                if (0 >= iChildrenCount)
                {
                    break;
                }

                for (int i = 0; i < iChildrenCount; ++i)
                {
                    DependencyObject objChild = VisualTreeHelper.GetChild(objReference, i);
                    if (null != objChild && objChild is T)
                    {
                        objRetval = (T)objChild;
                        break;
                    }

                    T objGrandChild = VisualTreeHelperEx.GetChildObject <T>(objChild);
                    if (null != objGrandChild)
                    {
                        objRetval = objGrandChild;
                        break;
                    }
                }
            }while (false);

            return(objRetval);
        }