@@ -153,44 +153,46 @@ public static class LogicalTree
153
153
}
154
154
}
155
155
}
156
- else if ( element is UserControl userControl )
156
+ else if ( element is ContentControl contentControl )
157
157
{
158
- if ( userControl . Content is T result && predicate . Match ( result ) )
158
+ if ( contentControl . Content is T result && predicate . Match ( result ) )
159
159
{
160
160
return result ;
161
161
}
162
162
163
- if ( userControl . Content is FrameworkElement content )
163
+ if ( contentControl . Content is FrameworkElement content )
164
164
{
165
165
element = content ;
166
166
167
167
goto Start ;
168
168
}
169
169
}
170
- else if ( element is ContentControl contentControl )
170
+ else if ( element is Border border )
171
171
{
172
- if ( contentControl . Content is T result && predicate . Match ( result ) )
172
+ if ( border . Child is T result && predicate . Match ( result ) )
173
173
{
174
174
return result ;
175
175
}
176
176
177
- if ( contentControl . Content is FrameworkElement content )
177
+ if ( border . Child is FrameworkElement child )
178
178
{
179
- element = content ;
179
+ element = child ;
180
180
181
181
goto Start ;
182
182
}
183
183
}
184
- else if ( element is Border border )
184
+ else if ( element is UserControl userControl )
185
185
{
186
- if ( border . Child is T result && predicate . Match ( result ) )
186
+ // We put UserControl right before the slower reflection fallback path as
187
+ // this type is less likely to be used compared to the other ones above.
188
+ if ( userControl . Content is T result && predicate . Match ( result ) )
187
189
{
188
190
return result ;
189
191
}
190
192
191
- if ( border . Child is FrameworkElement child )
193
+ if ( userControl . Content is FrameworkElement content )
192
194
{
193
- element = child ;
195
+ element = content ;
194
196
195
197
goto Start ;
196
198
}
@@ -348,9 +350,9 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
348
350
}
349
351
}
350
352
}
351
- else if ( element is UserControl userControl )
353
+ else if ( element is ContentControl contentControl )
352
354
{
353
- if ( userControl . Content is FrameworkElement content )
355
+ if ( contentControl . Content is FrameworkElement content )
354
356
{
355
357
yield return content ;
356
358
@@ -359,24 +361,24 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
359
361
goto Start ;
360
362
}
361
363
}
362
- else if ( element is ContentControl contentControl )
364
+ else if ( element is Border border )
363
365
{
364
- if ( contentControl . Content is FrameworkElement content )
366
+ if ( border . Child is FrameworkElement child )
365
367
{
366
- yield return content ;
368
+ yield return child ;
367
369
368
- element = content ;
370
+ element = child ;
369
371
370
372
goto Start ;
371
373
}
372
374
}
373
- else if ( element is Border border )
375
+ else if ( element is UserControl userControl )
374
376
{
375
- if ( border . Child is FrameworkElement child )
377
+ if ( userControl . Content is FrameworkElement content )
376
378
{
377
- yield return child ;
379
+ yield return content ;
378
380
379
- element = child ;
381
+ element = content ;
380
382
381
383
goto Start ;
382
384
}
0 commit comments