Question

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;
}
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top