Extentions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Media3D;
  17. using System.Runtime.InteropServices;
  18. namespace Xceed.Wpf.AvalonDock.Controls
  19. {
  20. public static class Extentions
  21. {
  22. public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj) where T : DependencyObject
  23. {
  24. if (depObj != null)
  25. {
  26. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  27. {
  28. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  29. if (child != null && child is T)
  30. {
  31. yield return (T)child;
  32. }
  33. foreach (T childOfChild in FindVisualChildren<T>(child))
  34. {
  35. yield return childOfChild;
  36. }
  37. }
  38. }
  39. }
  40. public static IEnumerable<T> FindLogicalChildren<T>(this DependencyObject depObj) where T : DependencyObject
  41. {
  42. if (depObj != null)
  43. {
  44. foreach (DependencyObject child in LogicalTreeHelper.GetChildren(depObj).OfType<DependencyObject>())
  45. {
  46. if (child != null && child is T)
  47. {
  48. yield return (T)child;
  49. }
  50. foreach (T childOfChild in FindLogicalChildren<T>(child))
  51. {
  52. yield return childOfChild;
  53. }
  54. }
  55. }
  56. }
  57. public static DependencyObject FindVisualTreeRoot(this DependencyObject initial)
  58. {
  59. DependencyObject current = initial;
  60. DependencyObject result = initial;
  61. while (current != null)
  62. {
  63. result = current;
  64. if (current is Visual || current is Visual3D)
  65. {
  66. current = VisualTreeHelper.GetParent(current);
  67. }
  68. else
  69. {
  70. // If we're in Logical Land then we must walk
  71. // up the logical tree until we find a
  72. // Visual/Visual3D to get us back to Visual Land.
  73. current = LogicalTreeHelper.GetParent(current);
  74. }
  75. }
  76. return result;
  77. }
  78. public static T FindVisualAncestor<T>(this DependencyObject dependencyObject) where T : class
  79. {
  80. DependencyObject target = dependencyObject;
  81. do
  82. {
  83. target = VisualTreeHelper.GetParent(target);
  84. }
  85. while (target != null && !(target is T));
  86. return target as T;
  87. }
  88. public static T FindLogicalAncestor<T>(this DependencyObject dependencyObject) where T : class
  89. {
  90. DependencyObject target = dependencyObject;
  91. do
  92. {
  93. var current = target;
  94. target = LogicalTreeHelper.GetParent(target);
  95. if (target == null)
  96. target = VisualTreeHelper.GetParent(current);
  97. }
  98. while (target != null && !(target is T));
  99. return target as T;
  100. }
  101. }
  102. }