LinqToVisualTree.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. namespace LinqToVisualTree
  7. {
  8. /// <summary>
  9. /// Adapts a DependencyObject to provide methods required for generate
  10. /// a Linq To Tree API
  11. /// </summary>
  12. public class VisualTreeAdapter : ILinqTree<DependencyObject>
  13. {
  14. private DependencyObject _item;
  15. public VisualTreeAdapter(DependencyObject item)
  16. {
  17. _item = item;
  18. }
  19. public IEnumerable<DependencyObject> Children()
  20. {
  21. int childrenCount = VisualTreeHelper.GetChildrenCount(_item);
  22. for (int i = 0; i < childrenCount; i++)
  23. {
  24. yield return VisualTreeHelper.GetChild(_item, i);
  25. }
  26. }
  27. public DependencyObject Parent
  28. {
  29. get
  30. {
  31. return VisualTreeHelper.GetParent(_item);
  32. }
  33. }
  34. }
  35. }
  36. namespace LinqToVisualTree
  37. {
  38. /// <summary>
  39. /// Defines an interface that must be implemented to generate the LinqToTree methods
  40. /// </summary>
  41. /// <typeparam name="T"></typeparam>
  42. public interface ILinqTree<T>
  43. {
  44. IEnumerable<T> Children();
  45. T Parent { get; }
  46. }
  47. public static class TreeExtensions
  48. {
  49. /// <summary>
  50. /// Returns a collection of descendant elements.
  51. /// </summary>
  52. public static IEnumerable<DependencyObject> Descendants(this DependencyObject item)
  53. {
  54. ILinqTree<DependencyObject> adapter = new VisualTreeAdapter(item);
  55. foreach (var child in adapter.Children())
  56. {
  57. yield return child;
  58. foreach (var grandChild in child.Descendants())
  59. {
  60. yield return grandChild;
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Returns a collection containing this element and all descendant elements.
  66. /// </summary>
  67. public static IEnumerable<DependencyObject> DescendantsAndSelf(this DependencyObject item)
  68. {
  69. yield return item;
  70. foreach (var child in item.Descendants())
  71. {
  72. yield return child;
  73. }
  74. }
  75. /// <summary>
  76. /// Returns a collection of ancestor elements.
  77. /// </summary>
  78. public static IEnumerable<DependencyObject> Ancestors(this DependencyObject item)
  79. {
  80. ILinqTree<DependencyObject> adapter = new VisualTreeAdapter(item);
  81. var parent = adapter.Parent;
  82. while (parent != null)
  83. {
  84. yield return parent;
  85. adapter = new VisualTreeAdapter(parent);
  86. parent = adapter.Parent;
  87. }
  88. }
  89. /// <summary>
  90. /// Returns a collection containing this element and all ancestor elements.
  91. /// </summary>
  92. public static IEnumerable<DependencyObject> AncestorsAndSelf(this DependencyObject item)
  93. {
  94. yield return item;
  95. foreach (var ancestor in item.Ancestors())
  96. {
  97. yield return ancestor;
  98. }
  99. }
  100. /// <summary>
  101. /// Returns a collection of child elements.
  102. /// </summary>
  103. public static IEnumerable<DependencyObject> Elements(this DependencyObject item)
  104. {
  105. ILinqTree<DependencyObject> adapter = new VisualTreeAdapter(item);
  106. foreach (var child in adapter.Children())
  107. {
  108. yield return child;
  109. }
  110. }
  111. /// <summary>
  112. /// Returns a collection of the sibling elements before this node, in document order.
  113. /// </summary>
  114. public static IEnumerable<DependencyObject> ElementsBeforeSelf(this DependencyObject item)
  115. {
  116. if (item.Ancestors().FirstOrDefault() == null)
  117. yield break;
  118. foreach (var child in item.Ancestors().First().Elements())
  119. {
  120. if (child.Equals(item))
  121. break;
  122. yield return child;
  123. }
  124. }
  125. /// <summary>
  126. /// Returns a collection of the after elements after this node, in document order.
  127. /// </summary>
  128. public static IEnumerable<DependencyObject> ElementsAfterSelf(this DependencyObject item)
  129. {
  130. if (item.Ancestors().FirstOrDefault() == null)
  131. yield break;
  132. bool afterSelf = false;
  133. foreach (var child in item.Ancestors().First().Elements())
  134. {
  135. if (afterSelf)
  136. yield return child;
  137. if (child.Equals(item))
  138. afterSelf = true;
  139. }
  140. }
  141. /// <summary>
  142. /// Returns a collection containing this element and all child elements.
  143. /// </summary>
  144. public static IEnumerable<DependencyObject> ElementsAndSelf(this DependencyObject item)
  145. {
  146. yield return item;
  147. foreach (var child in item.Elements())
  148. {
  149. yield return child;
  150. }
  151. }
  152. /// <summary>
  153. /// Returns a collection of descendant elements which match the given type.
  154. /// </summary>
  155. public static IEnumerable<DependencyObject> Descendants<T>(this DependencyObject item)
  156. {
  157. return item.Descendants().Where(i => i is T).Cast<DependencyObject>();
  158. }
  159. /// <summary>
  160. /// Returns a collection of the sibling elements before this node, in document order
  161. /// which match the given type.
  162. /// </summary>
  163. public static IEnumerable<DependencyObject> ElementsBeforeSelf<T>(this DependencyObject item)
  164. {
  165. return item.ElementsBeforeSelf().Where(i => i is T).Cast<DependencyObject>();
  166. }
  167. /// <summary>
  168. /// Returns a collection of the after elements after this node, in document order
  169. /// which match the given type.
  170. /// </summary>
  171. public static IEnumerable<DependencyObject> ElementsAfterSelf<T>(this DependencyObject item)
  172. {
  173. return item.ElementsAfterSelf().Where(i => i is T).Cast<DependencyObject>();
  174. }
  175. /// <summary>
  176. /// Returns a collection containing this element and all descendant elements
  177. /// which match the given type.
  178. /// </summary>
  179. public static IEnumerable<DependencyObject> DescendantsAndSelf<T>(this DependencyObject item)
  180. {
  181. return item.DescendantsAndSelf().Where(i => i is T).Cast<DependencyObject>();
  182. }
  183. /// <summary>
  184. /// Returns a collection of ancestor elements which match the given type.
  185. /// </summary>
  186. public static IEnumerable<DependencyObject> Ancestors<T>(this DependencyObject item)
  187. {
  188. return item.Ancestors().Where(i => i is T).Cast<DependencyObject>();
  189. }
  190. /// <summary>
  191. /// Returns a collection containing this element and all ancestor elements
  192. /// which match the given type.
  193. /// </summary>
  194. public static IEnumerable<DependencyObject> AncestorsAndSelf<T>(this DependencyObject item)
  195. {
  196. return item.AncestorsAndSelf().Where(i => i is T).Cast<DependencyObject>();
  197. }
  198. /// <summary>
  199. /// Returns a collection of child elements which match the given type.
  200. /// </summary>
  201. public static IEnumerable<DependencyObject> Elements<T>(this DependencyObject item)
  202. {
  203. return item.Elements().Where(i => i is T).Cast<DependencyObject>();
  204. }
  205. /// <summary>
  206. /// Returns a collection containing this element and all child elements.
  207. /// which match the given type.
  208. /// </summary>
  209. public static IEnumerable<DependencyObject> ElementsAndSelf<T>(this DependencyObject item)
  210. {
  211. return item.ElementsAndSelf().Where(i => i is T).Cast<DependencyObject>();
  212. }
  213. }
  214. public static class EnumerableTreeExtensions
  215. {
  216. /// <summary>
  217. /// Applies the given function to each of the items in the supplied
  218. /// IEnumerable.
  219. /// </summary>
  220. private static IEnumerable<DependencyObject> DrillDown(this IEnumerable<DependencyObject> items,
  221. Func<DependencyObject, IEnumerable<DependencyObject>> function)
  222. {
  223. foreach (var item in items)
  224. {
  225. foreach (var itemChild in function(item))
  226. {
  227. yield return itemChild;
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// Applies the given function to each of the items in the supplied
  233. /// IEnumerable, which match the given type.
  234. /// </summary>
  235. public static IEnumerable<DependencyObject> DrillDown<T>(this IEnumerable<DependencyObject> items,
  236. Func<DependencyObject, IEnumerable<DependencyObject>> function)
  237. where T : DependencyObject
  238. {
  239. foreach (var item in items)
  240. {
  241. foreach (var itemChild in function(item))
  242. {
  243. if (itemChild is T)
  244. {
  245. yield return (T)itemChild;
  246. }
  247. }
  248. }
  249. }
  250. /// <summary>
  251. /// Returns a collection of descendant elements.
  252. /// </summary>
  253. public static IEnumerable<DependencyObject> Descendants(this IEnumerable<DependencyObject> items)
  254. {
  255. return items.DrillDown(i => i.Descendants());
  256. }
  257. /// <summary>
  258. /// Returns a collection containing this element and all descendant elements.
  259. /// </summary>
  260. public static IEnumerable<DependencyObject> DescendantsAndSelf(this IEnumerable<DependencyObject> items)
  261. {
  262. return items.DrillDown(i => i.DescendantsAndSelf());
  263. }
  264. /// <summary>
  265. /// Returns a collection of ancestor elements.
  266. /// </summary>
  267. public static IEnumerable<DependencyObject> Ancestors(this IEnumerable<DependencyObject> items)
  268. {
  269. return items.DrillDown(i => i.Ancestors());
  270. }
  271. /// <summary>
  272. /// Returns a collection containing this element and all ancestor elements.
  273. /// </summary>
  274. public static IEnumerable<DependencyObject> AncestorsAndSelf(this IEnumerable<DependencyObject> items)
  275. {
  276. return items.DrillDown(i => i.AncestorsAndSelf());
  277. }
  278. /// <summary>
  279. /// Returns a collection of child elements.
  280. /// </summary>
  281. public static IEnumerable<DependencyObject> Elements(this IEnumerable<DependencyObject> items)
  282. {
  283. return items.DrillDown(i => i.Elements());
  284. }
  285. /// <summary>
  286. /// Returns a collection containing this element and all child elements.
  287. /// </summary>
  288. public static IEnumerable<DependencyObject> ElementsAndSelf(this IEnumerable<DependencyObject> items)
  289. {
  290. return items.DrillDown(i => i.ElementsAndSelf());
  291. }
  292. /// <summary>
  293. /// Returns a collection of descendant elements which match the given type.
  294. /// </summary>
  295. public static IEnumerable<DependencyObject> Descendants<T>(this IEnumerable<DependencyObject> items)
  296. where T : DependencyObject
  297. {
  298. return items.DrillDown<T>(i => i.Descendants());
  299. }
  300. /// <summary>
  301. /// Returns a collection containing this element and all descendant elements.
  302. /// which match the given type.
  303. /// </summary>
  304. public static IEnumerable<DependencyObject> DescendantsAndSelf<T>(this IEnumerable<DependencyObject> items)
  305. where T : DependencyObject
  306. {
  307. return items.DrillDown<T>(i => i.DescendantsAndSelf());
  308. }
  309. /// <summary>
  310. /// Returns a collection of ancestor elements which match the given type.
  311. /// </summary>
  312. public static IEnumerable<DependencyObject> Ancestors<T>(this IEnumerable<DependencyObject> items)
  313. where T : DependencyObject
  314. {
  315. return items.DrillDown<T>(i => i.Ancestors());
  316. }
  317. /// <summary>
  318. /// Returns a collection containing this element and all ancestor elements.
  319. /// which match the given type.
  320. /// </summary>
  321. public static IEnumerable<DependencyObject> AncestorsAndSelf<T>(this IEnumerable<DependencyObject> items)
  322. where T : DependencyObject
  323. {
  324. return items.DrillDown<T>(i => i.AncestorsAndSelf());
  325. }
  326. /// <summary>
  327. /// Returns a collection of child elements which match the given type.
  328. /// </summary>
  329. public static IEnumerable<DependencyObject> Elements<T>(this IEnumerable<DependencyObject> items)
  330. where T : DependencyObject
  331. {
  332. return items.DrillDown<T>(i => i.Elements());
  333. }
  334. /// <summary>
  335. /// Returns a collection containing this element and all child elements.
  336. /// which match the given type.
  337. /// </summary>
  338. public static IEnumerable<DependencyObject> ElementsAndSelf<T>(this IEnumerable<DependencyObject> items)
  339. where T : DependencyObject
  340. {
  341. return items.DrillDown<T>(i => i.ElementsAndSelf());
  342. }
  343. }
  344. }