You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
publicvoidParseVisualTree(DependencyObjectreference){// do work on reference hereintcount=VisualTreeHelper.GetChildrenCount(reference);for(intindex=0;index<count;index++){DependencyObjectchild=VisualTreeHelper.GetChild(reference,index);ParseVisualTree(child);}}
The following work around does enumerate the contents of an Expander:
publicvoidParseVisualTree(DependencyObjectreference){// do work on reference hereif((referenceisContentPresentercp)&&(cp.ContentisDependencyObjectcontent)){ParseVisualTree(content);}else{intcount=VisualTreeHelper.GetChildrenCount(reference);for(intindex=0;index<count;index++){DependencyObjectchild=VisualTreeHelper.GetChild(reference,index);ParseVisualTree(child);}}}
The work around was derived from the following WindowsCommunityToolkit code:
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.
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.
The following work around does enumerate the contents of an Expander:
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
The text was updated successfully, but these errors were encountered: