0
我試圖在我的視圖(用戶控件)中獲取所有邏輯子項。我從根元素開始,遍歷邏輯樹,一切都按預期工作,但是,我的一些子控件是像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);
}
}
}