Skip to content

Commit 9fd53a6

Browse files
Merge pull request #3811 from michael-hawker/fix-switchpresenter-logicaltree
Add ContentPresenter to Logical Tree helpers
2 parents 4a594a1 + 85e8bf4 commit 9fd53a6

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/FrameworkElement/FrameworkElementExtensions.LogicalTree.cs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,43 +153,60 @@ public static partial class FrameworkElementExtensions
153153
}
154154
else if (element is ContentControl contentControl)
155155
{
156-
if (contentControl.Content is T result && predicate.Match(result))
157-
{
158-
return result;
159-
}
160-
161156
if (contentControl.Content is FrameworkElement content)
162157
{
158+
if (content is T result && predicate.Match(result))
159+
{
160+
return result;
161+
}
162+
163163
element = content;
164164

165165
goto Start;
166166
}
167167
}
168168
else if (element is Border border)
169169
{
170-
if (border.Child is T result && predicate.Match(result))
171-
{
172-
return result;
173-
}
174-
175170
if (border.Child is FrameworkElement child)
176171
{
172+
if (child is T result && predicate.Match(result))
173+
{
174+
return result;
175+
}
176+
177177
element = child;
178178

179179
goto Start;
180180
}
181181
}
182+
else if (element is ContentPresenter contentPresenter)
183+
{
184+
// Sometimes ContentPresenter is used in control templates instead of ContentControl,
185+
// therefore we should still check if its Content is a matching FrameworkElement instance.
186+
// This also makes this work for SwitchPresenter.
187+
if (contentPresenter.Content is FrameworkElement content)
188+
{
189+
if (content is T result && predicate.Match(result))
190+
{
191+
return result;
192+
}
193+
194+
element = content;
195+
196+
goto Start;
197+
}
198+
}
182199
else if (element is UserControl userControl)
183200
{
184201
// We put UserControl right before the slower reflection fallback path as
185202
// this type is less likely to be used compared to the other ones above.
186-
if (userControl.Content is T result && predicate.Match(result))
187-
{
188-
return result;
189-
}
190-
191203
if (userControl.Content is FrameworkElement content)
192204
{
205+
if (content is T result && predicate.Match(result))
206+
{
207+
return result;
208+
}
209+
193210
element = content;
194211

195212
goto Start;
@@ -370,6 +387,17 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
370387
goto Start;
371388
}
372389
}
390+
else if (element is ContentPresenter contentPresenter)
391+
{
392+
if (contentPresenter.Content is FrameworkElement content)
393+
{
394+
yield return content;
395+
396+
element = content;
397+
398+
goto Start;
399+
}
400+
}
373401
else if (element is UserControl userControl)
374402
{
375403
if (userControl.Content is FrameworkElement content)

0 commit comments

Comments
 (0)