Guest User

Untitled

a guest
Mar 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Media;
  5.  
  6. namespace ListViewScroll
  7. {
  8. public static class VisualTreeHelperExtensions
  9. {
  10. public static T GetFirstDescendantsOfType<T>(this DependencyObject obj) where T : DependencyObject
  11. {
  12. return obj.GetDescentantsOfType<T>().FirstOrDefault();
  13. }
  14. public static IEnumerable<T> GetDescentantsOfType<T>(this DependencyObject obj) where T : DependencyObject
  15. {
  16. return obj.GetDescendants().OfType<T>();
  17. }
  18. public static IEnumerable<DependencyObject> GetDescendants(this DependencyObject obj)
  19. {
  20. var queue = new Queue<DependencyObject>();
  21. var count = VisualTreeHelper.GetChildrenCount(obj);
  22. for(int i=0;i<count;++i)
  23. {
  24. var child = VisualTreeHelper.GetChild(obj, i);
  25. yield return child;
  26. queue.Enqueue(child);
  27. }
  28. while( queue.Any())
  29. {
  30. var parent = queue.Dequeue();
  31. var count2 = VisualTreeHelper.GetChildrenCount(parent);
  32.  
  33. for(int i=0;i<count2;++i)
  34. {
  35. var child = VisualTreeHelper.GetChild(parent, i);
  36. yield return child;
  37. queue.Enqueue(child);
  38. }
  39. }
  40. }
  41.  
  42. }
  43. }
Add Comment
Please, Sign In to add comment