Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media;
- namespace ListViewScroll
- {
- public static class VisualTreeHelperExtensions
- {
- public static T GetFirstDescendantsOfType<T>(this DependencyObject obj) where T : DependencyObject
- {
- return obj.GetDescentantsOfType<T>().FirstOrDefault();
- }
- public static IEnumerable<T> GetDescentantsOfType<T>(this DependencyObject obj) where T : DependencyObject
- {
- return obj.GetDescendants().OfType<T>();
- }
- public static IEnumerable<DependencyObject> GetDescendants(this DependencyObject obj)
- {
- var queue = new Queue<DependencyObject>();
- var count = VisualTreeHelper.GetChildrenCount(obj);
- for(int i=0;i<count;++i)
- {
- var child = VisualTreeHelper.GetChild(obj, i);
- yield return child;
- queue.Enqueue(child);
- }
- while( queue.Any())
- {
- var parent = queue.Dequeue();
- var count2 = VisualTreeHelper.GetChildrenCount(parent);
- for(int i=0;i<count2;++i)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
- yield return child;
- queue.Enqueue(child);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment