【发布时间】:2015-05-16 04:30:50
【问题描述】:
我试图在我的视图中获取所有逻辑子项(用户控件)。我从根元素开始,遍历逻辑树,一切都按预期工作,但是,我的几个子控件是 ListBox 等项目,它们是数据绑定的并为它们的孩子使用数据模板,这些项目没有在逻辑树中返回。
这是我正在使用的代码:
private static void GetLogicalChildren<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
{
IEnumerable children = LogicalTreeHelper.GetChildren(parent);
foreach (object child in children)
{
if (child is DependencyObject)
{
DependencyObject depChild = child as DependencyObject;
if (child is T)
{
logicalCollection.Add(child as T);
}
GetLogicalChildren(depChild, logicalCollection);
}
}
}
【问题讨论】:
标签: wpf wpf-controls