mirror of
https://github.com/Stone-Red-Code/docs-desktop.git
synced 2026-09-05 16:06:10 +02:00
* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
28 lines
2.5 KiB
Markdown
28 lines
2.5 KiB
Markdown
---
|
|
title: "How to: Define and Reference a Resource"
|
|
ms.date: "03/30/2017"
|
|
helpviewer_keywords:
|
|
- "resources [WPF], defining"
|
|
- "defining resources [WPF]"
|
|
- "resources [WPF], referencing"
|
|
- "referencing resources [WPF]"
|
|
ms.assetid: b86b876b-0a10-489b-9a5d-581ea9b32406
|
|
---
|
|
|
|
# How to: Define and Reference a Resource
|
|
|
|
This example shows how to define a resource and reference it by using an attribute in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)].
|
|
|
|
## Example
|
|
|
|
The following example defines two types of resources: a <xref:System.Windows.Media.SolidColorBrush> resource, and several <xref:System.Windows.Style> resources. The <xref:System.Windows.Media.SolidColorBrush> resource `MyBrush` is used to provide the value of several properties that each take a <xref:System.Windows.Media.Brush> type value. The <xref:System.Windows.Style> resources `PageBackground`, `TitleText` and `Label` each target a particular control type. The styles set a variety of different properties on the targeted controls, when that style resource is referenced by resource key and is used to set the <xref:System.Windows.FrameworkElement.Style%2A> property of several specific control elements defined in [!INCLUDE[TLA2#tla_xaml](../../../includes/tla2sharptla-xaml-md.md)].
|
|
|
|
Note that one of the properties within the setters of the `Label` style also references the `MyBrush` resource defined earlier. This is a common technique, but it is important to remember that resources are parsed and entered into a resource dictionary in the order that they are given. Resources are also requested by the order found within the dictionary if you use the [StaticResource Markup Extension](staticresource-markup-extension.md) to reference them from within another resource. Make sure that any resource that you reference is defined earlier within the resources collection than where that resource is then requested. If necessary, you can work around the strict creation order of resource references by using a [DynamicResource Markup Extension](dynamicresource-markup-extension.md) to reference the resource at runtime instead, but you should be aware that this DynamicResource technique has performance consequences. For details, see [XAML Resources](/dotnet/desktop-wpf/fundamentals/xaml-resources-define).
|
|
|
|
[!code-xaml[FEResource#XAML](~/samples/snippets/csharp/VS_Snippets_Wpf/FEResource/CS/default.xaml#xaml)]
|
|
|
|
## See also
|
|
|
|
- [XAML Resources](/dotnet/desktop-wpf/fundamentals/xaml-resources-define)
|
|
- [Styling and Templating](/dotnet/desktop-wpf/fundamentals/styles-templates-overview)
|