@@ -672,11 +672,10 @@ public static object FindResource(this FrameworkElement element, object resource
672672 /// </summary>
673673 /// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
674674 /// <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 )
678677 {
679- value = null ;
678+ object ? value = null ;
680679
681680 FrameworkElement ? current = element ;
682681
@@ -687,15 +686,34 @@ public static bool TryFindResource(this FrameworkElement element, object resourc
687686 {
688687 if ( current . Resources ? . TryGetValue ( resourceKey , out value ) == true )
689688 {
690- return true ;
689+ return value ;
691690 }
692691
693692 current = current . Parent as FrameworkElement ;
694693 }
695694 while ( current is not null ) ;
696695
697696 // 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 ;
699717 }
700718 }
701719}
0 commit comments