星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VTHelper.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections.Generic;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. namespace XHZB.Desktop.Utils
  5. {
  6. internal class VTHelper
  7. {
  8. public static T FindChild<T>(DependencyObject parent, string childName)
  9. where T : DependencyObject
  10. {
  11. if (parent == null)
  12. {
  13. return null;
  14. }
  15. T foundChild = null;
  16. int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  17. for (int i = 0; i < childrenCount; i++)
  18. {
  19. DependencyObject child = VisualTreeHelper.GetChild(parent, i);
  20. // 如果子控件不是需查找的控件类型
  21. T childType = child as T;
  22. if (childType == null)
  23. {
  24. // 在下一级控件中递归查找
  25. foundChild = FindChild<T>(child, childName);
  26. // 找到控件就可以中断递归操作
  27. if (foundChild != null)
  28. {
  29. break;
  30. }
  31. }
  32. else if (!string.IsNullOrEmpty(childName))
  33. {
  34. FrameworkElement frameworkElement = child as FrameworkElement;
  35. // 如果控件名称符合参数条件
  36. if (frameworkElement != null && frameworkElement.Name == childName)
  37. {
  38. foundChild = (T)child;
  39. break;
  40. }
  41. }
  42. else
  43. {
  44. // 查找到了控件
  45. foundChild = (T)child;
  46. break;
  47. }
  48. }
  49. return foundChild;
  50. }
  51. public static List<T> FindChilds<T>(DependencyObject parent, string childName)
  52. where T : DependencyObject
  53. {
  54. List<T> list = new List<T>();
  55. if (parent == null)
  56. {
  57. return list;
  58. }
  59. int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  60. for (int i = 0; i < childrenCount; i++)
  61. {
  62. DependencyObject child = VisualTreeHelper.GetChild(parent, i);
  63. // 如果子控件不是需查找的控件类型
  64. T childType = child as T;
  65. if (childType == null)
  66. {
  67. // 在下一级控件中递归查找
  68. List<T> findChildList = FindChilds<T>(child, childName);
  69. for (int j = 0; j < findChildList.Count; j++)
  70. {
  71. }
  72. list.AddRange(FindChilds<T>(child, childName));
  73. }
  74. else if (!string.IsNullOrEmpty(childName))
  75. {
  76. FrameworkElement frameworkElement = child as FrameworkElement;
  77. // 如果控件名称符合参数条件
  78. if (frameworkElement != null && frameworkElement.Name == childName)
  79. {
  80. list.Add((T)child);
  81. }
  82. }
  83. else
  84. {
  85. // 查找到了控件
  86. list.Add((T)child);
  87. }
  88. }
  89. return list;
  90. }
  91. /// <summary>
  92. /// 查找父元素
  93. /// </summary>
  94. /// <typeparam name="T"></typeparam>
  95. /// <param name="obj"></param>
  96. /// <param name="name"></param>
  97. /// <returns></returns>
  98. public static T FindParent<T>(DependencyObject i_dp) where T : DependencyObject
  99. {
  100. DependencyObject dobj = VisualTreeHelper.GetParent(i_dp);
  101. if (dobj != null)
  102. {
  103. if (dobj is T)
  104. {
  105. return (T)dobj;
  106. }
  107. else
  108. {
  109. dobj = FindParent<T>(dobj);
  110. if (dobj != null && dobj is T)
  111. {
  112. return (T)dobj;
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. }
  119. }