CommonHelper.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Media;
  7. using System.Windows;
  8. namespace ComPDFKit.Tool.Help
  9. {
  10. public static class CommonHelper
  11. {
  12. /// <summary>
  13. /// Find the parent control of the target type of the object
  14. /// </summary>
  15. /// <typeparam name="T"></typeparam>
  16. /// <param name="obj"></param>
  17. /// <returns></returns>
  18. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  19. {
  20. try
  21. {
  22. while (obj != null)
  23. {
  24. if (obj is T)
  25. return obj as T;
  26. obj = VisualTreeHelper.GetParent(obj);
  27. }
  28. return null;
  29. }
  30. catch { return null; }
  31. }
  32. /// <summary>
  33. /// Find the child control of the target type of the object
  34. /// </summary>
  35. /// <typeparam name="childItem">
  36. /// The type of the child control to find
  37. /// </typeparam>
  38. /// <param name="obj">
  39. /// The object to find
  40. /// </param>
  41. /// <returns>
  42. /// The child control of the target type of the object
  43. /// </returns>
  44. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  45. where childItem : DependencyObject
  46. {
  47. try
  48. {
  49. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  50. {
  51. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  52. if (child != null && child is childItem)
  53. return (childItem)child;
  54. else
  55. {
  56. childItem childOfChild = FindVisualChild<childItem>(child);
  57. if (childOfChild != null)
  58. return childOfChild;
  59. }
  60. }
  61. return null;
  62. }
  63. catch { return null; }
  64. }
  65. /// <summary>
  66. /// Find the child control of the target type of the object
  67. /// </summary>
  68. /// <typeparam name="childItem">
  69. /// The type of the child control to find
  70. /// </typeparam>
  71. /// <param name="obj">
  72. /// The object to find
  73. /// </param>
  74. /// <returns>
  75. /// The child control of the target type of the object
  76. /// </returns>
  77. public static List<childItem> FindVisualChildList<childItem>(DependencyObject obj)
  78. where childItem : DependencyObject
  79. {
  80. List <childItem> children = new List <childItem>();
  81. try
  82. {
  83. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  84. {
  85. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  86. if (child != null && child is childItem)
  87. {
  88. children.Add((childItem)child);
  89. }
  90. else
  91. {
  92. childItem childOfChild = FindVisualChild<childItem>(child);
  93. if (childOfChild != null)
  94. children.Add(childOfChild);
  95. }
  96. }
  97. return children;
  98. }
  99. catch { return children; }
  100. }
  101. }
  102. }