@@ -153,43 +153,60 @@ public static partial class FrameworkElementExtensions
153
153
}
154
154
else if ( element is ContentControl contentControl )
155
155
{
156
- if ( contentControl . Content is T result && predicate . Match ( result ) )
157
- {
158
- return result ;
159
- }
160
-
161
156
if ( contentControl . Content is FrameworkElement content )
162
157
{
158
+ if ( content is T result && predicate . Match ( result ) )
159
+ {
160
+ return result ;
161
+ }
162
+
163
163
element = content ;
164
164
165
165
goto Start ;
166
166
}
167
167
}
168
168
else if ( element is Border border )
169
169
{
170
- if ( border . Child is T result && predicate . Match ( result ) )
171
- {
172
- return result ;
173
- }
174
-
175
170
if ( border . Child is FrameworkElement child )
176
171
{
172
+ if ( child is T result && predicate . Match ( result ) )
173
+ {
174
+ return result ;
175
+ }
176
+
177
177
element = child ;
178
178
179
179
goto Start ;
180
180
}
181
181
}
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
+ }
182
199
else if ( element is UserControl userControl )
183
200
{
184
201
// We put UserControl right before the slower reflection fallback path as
185
202
// 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
-
191
203
if ( userControl . Content is FrameworkElement content )
192
204
{
205
+ if ( content is T result && predicate . Match ( result ) )
206
+ {
207
+ return result ;
208
+ }
209
+
193
210
element = content ;
194
211
195
212
goto Start ;
@@ -370,6 +387,17 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
370
387
goto Start ;
371
388
}
372
389
}
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
+ }
373
401
else if ( element is UserControl userControl )
374
402
{
375
403
if ( userControl . Content is FrameworkElement content )
0 commit comments