@@ -672,11 +672,10 @@ public static object FindResource(this FrameworkElement element, object resource
672
672
/// </summary>
673
673
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
674
674
/// <param name="resourceKey">The resource key to search for.</param>
675
- /// <param name="value">The resulting value, if present.</param>
676
- /// <returns>Whether or not a value with the specified key has been found.</returns>
677
- public static bool TryFindResource ( this FrameworkElement element , object resourceKey , out object ? value )
675
+ /// <returns>The requested resource, or <see langword="null"/> if it wasn't found.</returns>
676
+ public static object ? TryFindResource ( this FrameworkElement element , object resourceKey )
678
677
{
679
- value = null ;
678
+ object ? value = null ;
680
679
681
680
FrameworkElement ? current = element ;
682
681
@@ -687,15 +686,34 @@ public static bool TryFindResource(this FrameworkElement element, object resourc
687
686
{
688
687
if ( current . Resources ? . TryGetValue ( resourceKey , out value ) == true )
689
688
{
690
- return true ;
689
+ return value ;
691
690
}
692
691
693
692
current = current . Parent as FrameworkElement ;
694
693
}
695
694
while ( current is not null ) ;
696
695
697
696
// Finally try application resources
698
- return Application . Current ? . Resources ? . TryGetValue ( resourceKey , out value ) == true ;
697
+ _ = Application . Current ? . Resources ? . TryGetValue ( resourceKey , out value ) ;
698
+
699
+ return value ;
700
+ }
701
+
702
+ /// <summary>
703
+ /// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
704
+ /// If the key is not found in the current element's resources, the logical tree is then
705
+ /// searched element-by-element to look for the resource in each element's resources.
706
+ /// If none of the elements contain the resource, the Application's resources are then searched.
707
+ /// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>.</para>
708
+ /// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>.</para>
709
+ /// </summary>
710
+ /// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
711
+ /// <param name="resourceKey">The resource key to search for.</param>
712
+ /// <param name="value">The resulting value, if present.</param>
713
+ /// <returns>Whether or not a value with the specified key has been found.</returns>
714
+ public static bool TryFindResource ( this FrameworkElement element , object resourceKey , out object ? value )
715
+ {
716
+ return ( value = TryFindResource ( element , resourceKey ) ) is not null ;
699
717
}
700
718
}
701
719
}
0 commit comments