Skip to main content
.NET Framework Class Library
FrameworkElementFindResource Method

Searches for a resource with the specified key, and throws an exception if the requested resource is not found.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
Public Function FindResource ( _
	resourceKey As [%$TOPIC/ms598123_en-us_VS_110_1_0_0_0_0%] _
) As [%$TOPIC/ms598123_en-us_VS_110_1_0_0_0_1%]
public [%$TOPIC/ms598123_en-us_VS_110_1_0_1_0_0%] FindResource(
	[%$TOPIC/ms598123_en-us_VS_110_1_0_1_0_1%] resourceKey
[%$TOPIC/ms598123_en-us_VS_110_1_0_2_0_0%]^ FindResource(
	[%$TOPIC/ms598123_en-us_VS_110_1_0_2_0_1%]^ resourceKey
member FindResource : 
        resourceKey:[%$TOPIC/ms598123_en-us_VS_110_1_0_3_0_0%] -> [%$TOPIC/ms598123_en-us_VS_110_1_0_3_0_1%]


Type: SystemObject

The key identifier for the requested resource.

Return Value

Type: SystemObject
The requested resource. If no resource with the provided key was found, an exception is thrown. An UnsetValue value might also be returned in the exception case.

resourceKey was not found and an event handler does not exist for the UnhandledException event.


resourceKey was not found and the Handled property is false in the UnhandledException event.


resourceKey is .

Important noteImportant

If you call this method for a key that cannot be found, an exception is thrown. If you do not want to handle exceptions that result from calling FindResource, call TryFindResource instead. TryFindResource returns when a requested resource cannot be found, and does not throw an exception.

If the resource is not found on the calling element, the parent element in the logical tree is searched next, then the application, then themes, and finally system resources. This lookup methodology is identical to how the tree is searched if a resource were requested by a dynamic resource reference in markup. For more information about resource lookup, see XAML Resources.

Typically, you immediately cast a FindResource return value to the type of the property that you setting with the returned resource value.

Resource keys are not necessarily strings. For instance, styles for controls at the theme level are deliberately keyed to the Type of the control, and application or page styles for controls typically use this same key convention. For details, see Styling and Templating or XAML Resources.


The following example obtains a named resource and casts it to an appropriate type to fill a property.

	Private Sub SetBGByResource(ByVal sender As Object, ByVal e As RoutedEventArgs)
	  Dim b As Button = TryCast(sender, Button)
	  b.Background = CType(Me.FindResource("RainbowBrush"), Brush)
	End Sub
void SetBGByResource(object sender, RoutedEventArgs e)
  Button b = sender as Button;
  b.Background = (Brush)this.FindResource("RainbowBrush");
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.