我在WPF程序中经常使用此功能来查找子元素:
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject{ if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {DependencyObject child = VisualTreeHelper.GetChild(depObj, i);if (child != null && child is T) yield return (T)child;foreach (T childOfChild in FindVisualChildren<T>(child)) yield return childOfChild; } }}
用法:
foreach (var rectangle in FindVisualChildren<Rectangle>(this)){ if (rectangle.Name == "rectangleBarChart") { }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)