This documentation is archived and is not being maintained.

ResourceDictionary Class

Provides a hash table / dictionary implementation that contains WPF resources used by components and other elements of a WPF application. 

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

public class ResourceDictionary : IDictionary, 
	ICollection, IEnumerable, ISupportInitialize, IUriContext, INameScope
<ResourceDictionary .../>

XAML Values


A property specifying a property element, where the value type of that property is ResourceDictionary. Typically, this is the Resources property of FrameworkElement or FrameworkContentElement, or of Application in an application resources case.


One or more resources specified as object elements. Each resource that you specify must have an x:Key Directive assigned.

The ResourceDictionary type exposes the following members.

Public methodResourceDictionaryInitializes a new instance of the ResourceDictionary class.

Public propertyCountGets the number of entries in the base ResourceDictionary.
Public propertyDeferrableContentInfrastructure. Gets or sets the deferrable content for this resource dictionary.
Public propertyIsFixedSizeGets whether this ResourceDictionary is fixed-size.
Public propertyIsReadOnlyGets whether this ResourceDictionary is read-only.
Public propertyItem Gets or sets the value associated with the given key.
Public propertyKeysGets a collection of all keys contained in this ResourceDictionary.
Public propertyMergedDictionariesGets a collection of the ResourceDictionary dictionaries that constitute the various resource dictionaries in the merged dictionaries.
Public propertySourceGets or sets the uniform resource identifier (URI) to load resources from.
Public propertyValues Gets a collection of all values associated with keys contained in this ResourceDictionary.

Public methodAddAdds a resource by key to this ResourceDictionary.
Public methodBeginInitBegins the initialization phase for this ResourceDictionary.
Public methodClearClears all keys (and values) in the base ResourceDictionary. This does not clear any merged dictionary items.
Public methodContainsDetermines whether the ResourceDictionary contains an element with the specified key.
Public methodCopyToCopies the ResourceDictionary elements to a one-dimensional DictionaryEntry at the specified index.
Public methodEndInitEnds the initialization phase, and invalidates the previous tree such that all changes made to keys during the initialization phase can be accounted for.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFindNameNot supported by this Dictionary implementation.
Public methodGetEnumeratorReturns an IDictionaryEnumerator that can be used to iterate through the ResourceDictionary.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRegisterNameNot supported by this Dictionary implementation.
Public methodRemoveRemoves the entry with the specified key from the base dictionary.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodUnregisterNameNot supported by this Dictionary implementation.

Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Explicit interface implemetationPrivate methodICollection.CopyToInfrastructure. For a description of this member, see ICollection.CopyTo.
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedInfrastructure. For a description of this member, see ICollection.IsSynchronized.
Explicit interface implemetationPrivate propertyICollection.SyncRootFor a description of this member, see ICollection.SyncRoot.
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorInfrastructure. For a description of this member, see IEnumerable.GetEnumerator.
Explicit interface implemetationPrivate propertyIUriContext.BaseUriFor a description of this member, see IUriContext.BaseUri.

The items in a ResourceDictionary are not immediately processed when application code is loaded by a XAML loader. Instead, the ResourceDictionary persists as an object, and the individual values are processed only when they are specifically requested.

The ResourceDictionary class is not derived from DictionaryBase. Instead, the ResourceDictionary class implements IDictionary but relies on a Hashtable internally.

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax In Detail. An exception is when you want to specify a merged dictionary; for details, see Merged Resource Dictionaries.

Another possible XAML usage is to declare a resource dictionary as a discrete XAML file, and either load it at run time with Load or include it in a (full-trust) project as a resource or loose file. In this case, ResourceDictionary can be declared as an object element, serving as the root element of the XAML. You must map the appropriate XML namespace values (default for the WPF namespace and typically x: for the XAML namespace) onto the ResourceDictionary element if you plan to use it as the root element. Then you can add child elements that define the resources, each with an x:Key value.

This example shows how to use application resources.

The following example shows an application definition file. The application definition file defines a resource section (a value for the Resources property). Resources defined at the application level can be accessed by all other pages that are part of the application. In this case, the resource is a declared style. Because a complete style that includes a control template can be lengthy, this example omits the control template that is defined within the ContentTemplate property setter of the style.

  <Style TargetType="Button" x:Key="GelButton" >
    <Setter Property="Margin" Value="1,2,1,2"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Template">



The following example shows a XAML page that references the application-level resource that the previous example defined. The resource is referenced by using a StaticResource Markup Extension that specifies the unique resource key for the requested resource. No resource with key of "GelButton" is found in the current page, so the resource lookup scope for the requested resource continues beyond the current page and into the defined application-level resources.

  <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
  <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.