回复 10楼还没想好的帖子
- public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
- {
- DependencyObject child = null;
- List<T> childList = new List<T>();
- for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
- {
- child = VisualTreeHelper.GetChild(obj, i);
- if (child is T && (((T)child).GetType() == typename))
- {
- childList.Add((T)child);
- }
- childList.AddRange(GetChildObjects<T>(child, typename));
- }
- return childList;
- }
复制代码 这是在WPF中查找子控件的方法,然后查找C1DockControl控件里的C1LinePanel子控件,并将其背景设置成你想要的颜色即可;
- List<C1LinePanel> a = GetChildObjects<C1LinePanel>(c1DockControl, typeof(C1LinePanel));
- a[0].Background = new SolidColorBrush(Colors.Blue);
复制代码 |