COMMON.cs 1.55 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace FLY.ControlLibrary
{
    public static class COMMON
    {
        public static Window GetWindow(DependencyObject dobj)
        {
            return GetParent<Window>(dobj);
            //DependencyObject dobj_window = dobj;
            ////一直向上找,直到找到window
            //do
            //{
            //    if (dobj_window is Window)
            //    {
            //        break;
            //    }
            //    dobj_window = System.Windows.Media.VisualTreeHelper.GetParent(dobj_window);
            //} while (dobj_window != null);
            //return dobj_window as Window;
        }

        public static T GetParent<T>(DependencyObject dobj) where T : DependencyObject
        {
            //一直向上找,直到找到window
            do
            {
                if (dobj is T)
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);
            return (T)dobj;
        }
        public static T GetParent<T>(DependencyObject dobj, string name) where T : FrameworkElement
        {
            do
            {
                if ((dobj is T) && (((T)dobj).Name == name))
                {
                    break;
                }
                dobj = System.Windows.Media.VisualTreeHelper.GetParent(dobj);
            } while (dobj != null);
            return (T)dobj;
        }
    }
}