Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.FindResource Method

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

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

public Object FindResource (
	Object resourceKey
)
public Object FindResource (
	Object resourceKey
)
public function FindResource (
	resourceKey : Object
) : Object
You cannot use methods in XAML.

Parameters

resourceKey

The key identifier for the requested resource.

Return Value

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

Exception typeCondition

ResourceReferenceKeyNotFoundException

The requested resource key was not found.

ArgumentNullException

Key was provided as a null reference (Nothing in Visual Basic).

NoteImportant:

If you call this method for a key that cannot be found, an exception is raised. If you do not want to handle exceptions that result from calling FindResource, call TryFindResource instead. TryFindResource returns a null reference (Nothing in Visual Basic) when a requested resource cannot be found, and does not raise 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 Resources Overview.

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 Styles Overview or Resources Overview.

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

void SetBGByResource(object sender, RoutedEventArgs e)
{
  Button b = sender as Button;
  b.Background = (Brush)this.FindResource("RainbowBrush");
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft