You are on page 1of 2

12/19/13

How to: Define and Reference a Resource

How to: Define and Reference a Resource


.NET Framework 4.5 This example shows how to define a resource and reference it by using an attribute in Extensible Application Markup Language (XAML).

Example
The following example defines two types of resources: a SolidColorBrush resource, and several Style resources. The SolidColorBrush resource M y B r u s his used to provide the value of several properties that each take a Brush type value. The Style resources P a g e B a c k g r o u n d ,T i t l e T e x tand L a b e leach 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 Style property of several specific control elements defined in XAML. Note that one of the properties within the setters of the L a b e lstyle also references the M y B r u s hresource 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 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 refererences by using a DynamicResource Markup Extension to reference the resource at runtime instead, but you should be aware that this DynamicResource technique has performance consequences. For details, see XAML Resources. XAML < P a g eN a m e = " r o o t " x m l n s = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / w i n f x / 2 0 0 6 / x a m l / p r e s e n t a t i o n " x m l n s : x = " h t t p : / / s c h e m a s . m i c r o s o f t . c o m / w i n f x / 2 0 0 6 / x a m l " > < P a g e . R e s o u r c e s > < S o l i d C o l o r B r u s hx : K e y = " M y B r u s h "C o l o r = " G o l d " / > < S t y l eT a r g e t T y p e = " B o r d e r "x : K e y = " P a g e B a c k g r o u n d " > < S e t t e rP r o p e r t y = " B a c k g r o u n d "V a l u e = " B l u e " / > < / S t y l e > < S t y l eT a r g e t T y p e = " T e x t B l o c k "x : K e y = " T i t l e T e x t " > < S e t t e rP r o p e r t y = " B a c k g r o u n d "V a l u e = " B l u e " / > < S e t t e rP r o p e r t y = " D o c k P a n e l . D o c k "V a l u e = " T o p " / > < S e t t e rP r o p e r t y = " F o n t S i z e "V a l u e = " 1 8 " / > < S e t t e rP r o p e r t y = " F o r e g r o u n d "V a l u e = " # 4 E 8 7 D 4 " / > < S e t t e rP r o p e r t y = " F o n t F a m i l y "V a l u e = " T r e b u c h e tM S " / > < S e t t e rP r o p e r t y = " M a r g i n "V a l u e = " 0 , 4 0 , 1 0 , 1 0 " / > < / S t y l e > < S t y l eT a r g e t T y p e = " T e x t B l o c k "x : K e y = " L a b e l " > < S e t t e rP r o p e r t y = " D o c k P a n e l . D o c k "V a l u e = " R i g h t " / > < S e t t e rP r o p e r t y = " F o n t S i z e "V a l u e = " 8 " / > < S e t t e rP r o p e r t y = " F o r e g r o u n d "V a l u e = " { S t a t i c R e s o u r c eM y B r u s h } " / > < S e t t e rP r o p e r t y = " F o n t F a m i l y "V a l u e = " A r i a l " / > < S e t t e rP r o p e r t y = " F o n t W e i g h t "V a l u e = " B o l d " / > < S e t t e rP r o p e r t y = " M a r g i n "V a l u e = " 0 , 3 , 1 0 , 0 " / > < / S t y l e > < / P a g e . R e s o u r c e s > < S t a c k P a n e l > < B o r d e rS t y l e = " { S t a t i c R e s o u r c eP a g e B a c k g r o u n d } " > < D o c k P a n e l >

msdn.microsoft.com/en-us/library/ms752294(d=printer,v=vs.110).aspx

1/2

12/19/13

< D o c k P a n e l > < T e x t B l o c kS t y l e = " { S t a t i c R e s o u r c eT i t l e T e x t } " > T i t l e < / T e x t B l o c k > < T e x t B l o c kS t y l e = " { S t a t i c R e s o u r c eL a b e l } " > L a b e l < / T e x t B l o c k > < T e x t B l o c kD o c k P a n e l . D o c k = " T o p "H o r i z o n t a l A l i g n m e n t = " L e f t "F o n t S i z e = " 3 6 "F o r e g r o u n d = " { S t a t i c R e s o u < B u t t o nD o c k P a n e l . D o c k = " T o p "H o r i z o n t a l A l i g n m e n t = " L e f t "H e i g h t = " 3 0 "B a c k g r o u n d = " { S t a t i c R e s o u r c eM < E l l i p s eD o c k P a n e l . D o c k = " T o p "H o r i z o n t a l A l i g n m e n t = " L e f t "W i d t h = " 1 0 0 "H e i g h t = " 1 0 0 "F i l l = " { S t a t i c R e < / D o c k P a n e l > < / B o r d e r > < / S t a c k P a n e l > < / P a g e >

How to: Define and Reference a Resource

See Also
Concepts
XAML Resources Styling and Templating

2013 Microsoft. All rights reserved.

msdn.microsoft.com/en-us/library/ms752294(d=printer,v=vs.110).aspx

2/2

You might also like