문제

I used a VisualTree helper to get all the Visuals in my window but sometimes some certain controls are not listed in the returning list. That's because they are still not rendered, as far as I know, VisualTree enumeration will help only if the controls are already rendered.

Now I am trying to write a simple recursive method that will list all the Logical objects in a window instead so I can operate with them before they are even rendered.

So here is my first try to create something like this for the Logical tree :

public static List<DependencyObject> ListLogical( DependencyObject parent )
{
    var depList = new List<DependencyObject>();
    foreach ( var child in LogicalTreeHelper.GetChildren( parent ).OfType<DependencyObject>() )
    {
        depList.AddRange( ListLogical( child ) );
    }
    return depList;
}
도움이 되었습니까?

해결책

I've found the error and corrected it, Here is a working method for that propose:

public static List<DependencyObject> ListLogical( DependencyObject parent)
{
    var depList = new List<DependencyObject>
    {
        parent
    };
    foreach ( var child in LogicalTreeHelper.GetChildren( parent ).OfType<DependencyObject>() )
    {
        depList.AddRange( ListLogical( child ) );
    }
    return depList;
}

The mistake I done in my first method was that I didn't add the parent itself to the returning list.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top