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

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

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

                var objParent = VisualTreeHelper.GetParent(objReference);
                while (null != objParent)
                {
                    if (objParent is T && ((T)objParent).Name == strName)
                    {
                        objRetval = (T)objParent;
                        break;
                    }

                    objParent = VisualTreeHelper.GetParent(objParent);
                }
            }while (false);

            return(objRetval);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 查找特定类型的父对象
        /// </summary>
        public static T GetParentObject <T>(DependencyObject objReference) where T : DependencyObject
        {
            T objRetval = null;

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

                var objParent = VisualTreeHelper.GetParent(objReference);
                if (null == objParent)
                {
                    break;
                }

                if (objParent is T)
                {
                    objRetval = (T)objParent;
                    break;
                }

                objRetval = VisualTreeHelperEx.GetParentObject <T>(objParent);
            }while (false);

            return(objRetval);
        }