Application.TryFindResource Method

Searches for the specified resource.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public Object TryFindResource(
	Object resourceKey


Type: System.Object

The name of the resource to find.

Return Value

Type: System.Object
The requested resource object. If the requested resource is not found, a null reference is returned.

TryFindResource will first look in application-scope resources for the specified resource. Application-scope resources are managed by Application, and are exposed from the Resources property. If the specified resource is not found in the set of application-scope resources, TryFindResource then next searches the system resources. System resources are shell resources defined by the user, and include colors, fonts, and shell configurations. These are exposed from the SystemColors, SystemFonts, and SystemParameters types, respectively, as static properties. To use TryFindResource to acquire them, these types also expose resource key properties that are designed to be passed to TryFindResource; for example, IconWidthKey.

Because TryFindResource returns an object, you must cast the returned value to the appropriate type if the resource is found.

This method is thread safe and can be called from any thread.

The following example shows how to use TryFindResource to acquire a resource.

		<Image x:Key="ApplicationResource" Source="ApplicationResource.bmp" />
void tryFindResourceButton_Click(object sender, RoutedEventArgs e) {
  object resource = Application.Current.TryFindResource("ApplicationResource");
  // If resource found, do something with it 
  if (resource != null) {



.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft