@@ -283,6 +283,24 @@ public static class DependencyObjectExtensions
283
283
return FindDescendant ( element , name , comparisonType ) ;
284
284
}
285
285
286
+ /// <summary>
287
+ /// Find the first descendant (or self) of type <see cref="FrameworkElement"/> with a given name.
288
+ /// </summary>
289
+ /// <param name="element">The root element.</param>
290
+ /// <param name="name">The name of the element to look for.</param>
291
+ /// <param name="comparisonType">The comparison type to use to match <paramref name="name"/>.</param>
292
+ /// <param name="searchType">The search type to use to explore the visual tree.</param>
293
+ /// <returns>The descendant (or self) that was found, or <see langword="null"/>.</returns>
294
+ public static FrameworkElement ? FindDescendantOrSelf ( this DependencyObject element , string name , StringComparison comparisonType , SearchType searchType )
295
+ {
296
+ if ( element is FrameworkElement result && name . Equals ( result . Name , comparisonType ) )
297
+ {
298
+ return result ;
299
+ }
300
+
301
+ return FindDescendant ( element , name , comparisonType , searchType ) ;
302
+ }
303
+
286
304
/// <summary>
287
305
/// Find the first descendant (or self) element of a given type, using a depth-first search.
288
306
/// </summary>
@@ -300,6 +318,24 @@ public static class DependencyObjectExtensions
300
318
return FindDescendant < T > ( element ) ;
301
319
}
302
320
321
+ /// <summary>
322
+ /// Find the first descendant (or self) element of a given type.
323
+ /// </summary>
324
+ /// <typeparam name="T">The type of elements to match.</typeparam>
325
+ /// <param name="element">The root element.</param>
326
+ /// <param name="searchType">The search type to use to explore the visual tree.</param>
327
+ /// <returns>The descendant (or self) that was found, or <see langword="null"/>.</returns>
328
+ public static T ? FindDescendantOrSelf < T > ( this DependencyObject element , SearchType searchType )
329
+ where T : notnull , DependencyObject
330
+ {
331
+ if ( element is T result )
332
+ {
333
+ return result ;
334
+ }
335
+
336
+ return FindDescendant < T > ( element , searchType ) ;
337
+ }
338
+
303
339
/// <summary>
304
340
/// Find the first descendant (or self) element of a given type, using a depth-first search.
305
341
/// </summary>
@@ -316,6 +352,23 @@ public static class DependencyObjectExtensions
316
352
return FindDescendant ( element , type ) ;
317
353
}
318
354
355
+ /// <summary>
356
+ /// Find the first descendant (or self) element of a given type.
357
+ /// </summary>
358
+ /// <param name="element">The root element.</param>
359
+ /// <param name="type">The type of element to match.</param>
360
+ /// <param name="searchType">The search type to use to explore the visual tree.</param>
361
+ /// <returns>The descendant (or self) that was found, or <see langword="null"/>.</returns>
362
+ public static DependencyObject ? FindDescendantOrSelf ( this DependencyObject element , Type type , SearchType searchType )
363
+ {
364
+ if ( element . GetType ( ) == type )
365
+ {
366
+ return element ;
367
+ }
368
+
369
+ return FindDescendant ( element , type , searchType ) ;
370
+ }
371
+
319
372
/// <summary>
320
373
/// Find the first descendant (or self) element matching a given predicate, using a depth-first search.
321
374
/// </summary>
@@ -334,6 +387,25 @@ public static class DependencyObjectExtensions
334
387
return FindDescendant ( element , predicate ) ;
335
388
}
336
389
390
+ /// <summary>
391
+ /// Find the first descendant (or self) element matching a given predicate.
392
+ /// </summary>
393
+ /// <typeparam name="T">The type of elements to match.</typeparam>
394
+ /// <param name="element">The root element.</param>
395
+ /// <param name="predicate">The predicatee to use to match the descendant nodes.</param>
396
+ /// <param name="searchType">The search type to use to explore the visual tree.</param>
397
+ /// <returns>The descendant (or self) that was found, or <see langword="null"/>.</returns>
398
+ public static T ? FindDescendantOrSelf < T > ( this DependencyObject element , Func < T , bool > predicate , SearchType searchType )
399
+ where T : notnull , DependencyObject
400
+ {
401
+ if ( element is T result && predicate ( result ) )
402
+ {
403
+ return result ;
404
+ }
405
+
406
+ return FindDescendant ( element , predicate , searchType ) ;
407
+ }
408
+
337
409
/// <summary>
338
410
/// Find the first descendant (or self) element matching a given predicate, using a depth-first search.
339
411
/// </summary>
@@ -354,6 +426,27 @@ public static class DependencyObjectExtensions
354
426
return FindDescendant ( element , state , predicate ) ;
355
427
}
356
428
429
+ /// <summary>
430
+ /// Find the first descendant (or self) element matching a given predicate.
431
+ /// </summary>
432
+ /// <typeparam name="T">The type of elements to match.</typeparam>
433
+ /// <typeparam name="TState">The type of state to use when matching nodes.</typeparam>
434
+ /// <param name="element">The root element.</param>
435
+ /// <param name="state">The state to give as input to <paramref name="predicate"/>.</param>
436
+ /// <param name="predicate">The predicatee to use to match the descendant nodes.</param>
437
+ /// <param name="searchType">The search type to use to explore the visual tree.</param>
438
+ /// <returns>The descendant (or self) that was found, or <see langword="null"/>.</returns>
439
+ public static T ? FindDescendantOrSelf < T , TState > ( this DependencyObject element , TState state , Func < T , TState , bool > predicate , SearchType searchType )
440
+ where T : notnull , DependencyObject
441
+ {
442
+ if ( element is T result && predicate ( result , state ) )
443
+ {
444
+ return result ;
445
+ }
446
+
447
+ return FindDescendant ( element , state , predicate , searchType ) ;
448
+ }
449
+
357
450
/// <summary>
358
451
/// Find all descendant elements of the specified element. This method can be chained with
359
452
/// LINQ calls to add additional filters or projections on top of the returned results.
0 commit comments