欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF常用助手类:UIHelper

程序员文章站 2023-01-28 21:30:52
public static class UIHelper { public static DependencyObject FindAncestorOfTy...
public static class UIHelper
    {
        public static DependencyObject FindAncestorOfType(DependencyObject root)
        {
            while ((root != null) && !(root is T))
            {
                root = VisualTreeHelper.GetParent(root);
            }
            return root;
        }

        public static T GetFirstDescendantOfType(this DependencyObject start) where T : DependencyObject
        {
            return start.GetDescendantsOfType().FirstOrDefault();
        }

        public static IEnumerable GetDescendantsOfType(this DependencyObject start) where T : DependencyObject
        {
            return start.GetDescendants().OfType();
        }

        public static IEnumerable GetDescendants(this DependencyObject start)
        {
            var queue = new Queue();

            var popup = start as Popup;

            if (popup != null)
            {
                if (popup.Child != null)
                {
                    queue.Enqueue(popup.Child);
                    yield return popup.Child;
                }
            }
            else
            {
                var count = VisualTreeHelper.GetChildrenCount(start);

                for (int i = 0; i  0)
            {
                var parent = queue.Dequeue();

                popup = parent as Popup;

                if (popup != null)
                {
                    if (popup.Child != null)
                    {
                        queue.Enqueue(popup.Child);
                        yield return popup.Child;
                    }
                }
                else
                {
                    var count = VisualTreeHelper.GetChildrenCount(parent);

                    for (int i = 0; i  GetChildren(this DependencyObject parent)
        {
            var popup = parent as Popup;

            if (popup != null)
            {
                if (popup.Child != null)
                {
                    yield return popup.Child;
                    yield break;
                }
            }

            var count = VisualTreeHelper.GetChildrenCount(parent);

            for (int i = 0; i (UIElement element)
        {
            string elementCopyXaml = System.Windows.Markup.XamlWriter.Save(element);
            return (T)System.Windows.Markup.XamlReader.Parse(elementCopyXaml);
        }

        public static Point GetElementPosition(UIElement element, UIElement ElementReferTo)
        {
            return element.TransformToVisual(ElementReferTo).Transform(new Point(0, 0));
        }

        public static BitmapSource CaptureElement(FrameworkElement element)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Default);
            rtb.Render(element);
            return rtb;
        }
    }