Export (0) Print
Expand All

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)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[AmbientAttribute]
[LocalizabilityAttribute(LocalizationCategory.Ignore)]
[UsableDuringInitializationAttribute(true)]
public class ResourceDictionary : IDictionary, 
	ICollection, IEnumerable, ISupportInitialize, IUriContext, INameScope
<ResourceDictionary .../>
<object>
  <object.resourcesProperty>
    oneOrMoreResources
  </object.resourcesProperty>
</object>

XAML Values

resourcesProperty

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.

oneOrMoreResources

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.

  NameDescription
Public methodResourceDictionaryInitializes a new instance of the ResourceDictionary class.
Top

  NameDescription
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.
Top

  NameDescription
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 the default hash function. (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.)
Protected methodOnGettingValueOccurs when the ResourceDictionary receives a request for a resource.
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.
Top

  NameDescription
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.)
Top

  NameDescription
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.
Top

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.

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


...


      </Setter.Value>
    </Setter>
  </Style>
</Application.Resources>

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.

<StackPanel
  Name="root"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >
  <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
  <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />
</StackPanel>

.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.

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