Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisualTreeHelper routines fail to enumerate contents of a ContentPresenter #9243

Closed
DHancock opened this issue Jan 11, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@DHancock
Copy link

DHancock commented Jan 11, 2024

Describe the bug

I was trying to parse the contents of an Expander control but the expander drop down contents were not being returned. The Expander drop down is a ContentPresenter. The following code fails when the dependencey object is a ContentPresenter with VisualTreeHelper.GetChildrenCount() always returning zero.

    public void ParseVisualTree(DependencyObject reference)
    {
        // do work on reference here

        int count = VisualTreeHelper.GetChildrenCount(reference);

        for (int index = 0; index < count; index++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(reference, index);
            ParseVisualTree(child);
        }
    }

The following work around does enumerate the contents of an Expander:

    public void ParseVisualTree(DependencyObject reference)
    {
        // do work on reference here

        if ((reference is ContentPresenter cp) && (cp.Content is DependencyObject content))
        {
            ParseVisualTree(content);
        }
        else
        {
            int count = VisualTreeHelper.GetChildrenCount(reference);
    
            for (int index = 0; index < count; index++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(reference, index);
                ParseVisualTree(child);
            }
        }
    }

The work around was derived from the following WindowsCommunityToolkit code:

https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/6bfe8a5d21feb8818a8048b9fc41a84a70af8b0f/Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.LogicalTree.cs#L182-L199C1

The live visual tree explorer in Visual Studio correctly shows the contents of the Expander drop down.

(Edit: improve work around)

Steps to reproduce the bug

Attempt to parse the visual tree to find the controls contained in an Expander's drop down.

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.3: 1.4.231115000

Windows version

Windows 11 (22H2): Build 22621

Additional context

No response

@DHancock DHancock added the bug Something isn't working label Jan 11, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 11, 2024
@DHancock
Copy link
Author

Not a bug. My mistake, it's evident that the controls in the expander drop down are only added in to the visual tree when the expander is expanded for the first time, after that they are always there whether the expander is expanded or not.

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-triage Issue needs to be triaged by the area owners and removed needs-triage Issue needs to be triaged by the area owners labels Jan 13, 2024
@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants