今回のVisualTree の表示に使った道具は以下のクラス。
public class ElementTreeList
{
public delegate void Enumerate(DependencyObject target, int level);
private static void EnumerateVisualChildren(DependencyObject src, Enumerate func, int level)
{
for (int index = 0; index < VisualTreeHelper.GetChildrenCount(src); index++)
{
DependencyObject child = VisualTreeHelper.GetChild(src, index);
func(child, level);
EnumerateVisualChildren(child, func, level + 1);
}
}
public static void EnumerateVisualChildren(DependencyObject src, Enumerate func)
{
func(src, 0);
EnumerateVisualChildren(src, func, 1);
}
}
こんな呼び方ができます。
ElementTreeList.EnumerateVisualChildren(this.listView1,
delegate(DependencyObject target, int level)
{
Debug.WriteLine(string.Format("{0}:{1}",new string('-',level),target.ToString()));
}
);