@@ -110,7 +110,7 @@ public static class LogicalTree
110
110
}
111
111
}
112
112
}
113
- else if ( element . TryGetContentControl ( ) is FrameworkElement contentControl )
113
+ else if ( element . GetContentControl ( ) is FrameworkElement contentControl )
114
114
{
115
115
if ( contentControl is T result && predicate ( result ) )
116
116
{
@@ -179,7 +179,7 @@ public static class LogicalTree
179
179
}
180
180
}
181
181
}
182
- else if ( element . TryGetContentControl ( ) is FrameworkElement contentControl )
182
+ else if ( element . GetContentControl ( ) is FrameworkElement contentControl )
183
183
{
184
184
if ( contentControl is T result && predicate ( result , state ) )
185
185
{
@@ -328,7 +328,7 @@ public static IEnumerable<FrameworkElement> FindChildren(this FrameworkElement e
328
328
}
329
329
}
330
330
}
331
- else if ( element . TryGetContentControl ( ) is FrameworkElement contentControl )
331
+ else if ( element . GetContentControl ( ) is FrameworkElement contentControl )
332
332
{
333
333
yield return contentControl ;
334
334
@@ -560,11 +560,11 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
560
560
}
561
561
562
562
/// <summary>
563
- /// Tries to retrieve the content property of this element as defined by <see cref="ContentPropertyAttribute"/>.
563
+ /// Gets the content property of this element as defined by <see cref="ContentPropertyAttribute"/>, if available .
564
564
/// </summary>
565
565
/// <param name="element">The parent element.</param>
566
566
/// <returns>The retrieved content control, or <see langword="null"/> if not available.</returns>
567
- public static UIElement ? TryGetContentControl ( this FrameworkElement element )
567
+ public static UIElement ? GetContentControl ( this FrameworkElement element )
568
568
{
569
569
Type type = element . GetType ( ) ;
570
570
TypeInfo ? typeInfo = type . GetTypeInfo ( ) ;
@@ -595,15 +595,41 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
595
595
/// If the key is not found in the current element's resources, the logical tree is then
596
596
/// searched element-by-element to look for the resource in each element's resources.
597
597
/// If none of the elements contain the resource, the Application's resources are then searched.
598
- /// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/></para>
599
- /// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/></para>
598
+ /// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>. </para>
599
+ /// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>. </para>
600
600
/// </summary>
601
601
/// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
602
602
/// <param name="resourceKey">The resource key to search for.</param>
603
- /// <returns>The requested resource, or <see langword="null"/>.</returns>
604
- public static object ? TryFindResource ( this FrameworkElement element , object resourceKey )
603
+ /// <returns>The requested resource.</returns>
604
+ /// <exception cref="Exception">Thrown when no resource is found with the specified key.</exception>
605
+ public static object FindResource ( this FrameworkElement element , object resourceKey )
605
606
{
606
- object ? value = null ;
607
+ if ( TryFindResource ( element , resourceKey , out object ? value ) )
608
+ {
609
+ return value ! ;
610
+ }
611
+
612
+ static object Throw ( ) => throw new Exception ( "No resource was found with the specified key" ) ;
613
+
614
+ return Throw ( ) ;
615
+ }
616
+
617
+ /// <summary>
618
+ /// Provides a WPF compatible version of TryFindResource to provide a static resource lookup.
619
+ /// If the key is not found in the current element's resources, the logical tree is then
620
+ /// searched element-by-element to look for the resource in each element's resources.
621
+ /// If none of the elements contain the resource, the Application's resources are then searched.
622
+ /// <para>See: <seealso href="https://docs.microsoft.com/dotnet/api/system.windows.frameworkelement.tryfindresource"/>.</para>
623
+ /// <para>And also: <seealso href="https://docs.microsoft.com/dotnet/desktop-wpf/fundamentals/xaml-resources-define#static-resource-lookup-behavior"/>.</para>
624
+ /// </summary>
625
+ /// <param name="element">The <see cref="FrameworkElement"/> to start searching for the target resource.</param>
626
+ /// <param name="resourceKey">The resource key to search for.</param>
627
+ /// <param name="value">The resulting value, if present.</param>
628
+ /// <returns>Whether or not a value with the specified key has been found.</returns>
629
+ public static bool TryFindResource ( this FrameworkElement element , object resourceKey , out object ? value )
630
+ {
631
+ value = null ;
632
+
607
633
FrameworkElement ? current = element ;
608
634
609
635
// Look in our dictionary and then walk-up parents. We use a do-while loop here
@@ -613,17 +639,15 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
613
639
{
614
640
if ( current . Resources ? . TryGetValue ( resourceKey , out value ) == true )
615
641
{
616
- return value ! ;
642
+ return true ;
617
643
}
618
644
619
645
current = current . Parent as FrameworkElement ;
620
646
}
621
647
while ( current is not null ) ;
622
648
623
649
// Finally try application resources
624
- Application . Current ? . Resources ? . TryGetValue ( resourceKey , out value ) ;
625
-
626
- return value ;
650
+ return Application . Current ? . Resources ? . TryGetValue ( resourceKey , out value ) == true ;
627
651
}
628
652
}
629
653
}
0 commit comments