当前位置: 移动技术网 > IT编程>开发语言>.net > WPF常用助手类:UIHelper

WPF常用助手类:UIHelper

2018年03月11日  | 移动技术网IT编程  | 我要评论

平乡县教育局,捕风捉影的意思,我是歌手歌王之战

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;
        }
    }

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网