Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 FindResource Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
FrameworkElement..::.FindResource 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)
Visual Basic (Declaration)
Public Function FindResource ( _
    resourceKey As Object _
) As Object
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim resourceKey As Object
Dim returnValue As Object

returnValue = instance.FindResource(resourceKey)
C#
public Object FindResource(
    Object resourceKey
)
Visual C++
public:
Object^ FindResource(
    Object^ resourceKey
)
JScript
public function FindResource(
    resourceKey : Object
) : Object
XAML
You cannot use methods in XAML.

Parameters

resourceKey
Type: System..::.Object
The key identifier for the requested resource.

Return Value

Type: System..::.Object
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.
ExceptionCondition
ResourceReferenceKeyNotFoundException

The requested resource key was not found.

ArgumentNullException

resourceKey is nullNothingnullptra null reference (Nothing in Visual Basic).

Important noteImportant Note:

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 nullNothingnullptra null reference (Nothing in Visual Basic) 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 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 Styling and Templating or Resources Overview.

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

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker