Export (0) Print
Expand All

How to: Use System Resources to Style a Button

.NET Framework 3.0

This example shows how to use system resources to style a Button.

Example

When you create a Button, you are not required to name a Style; however, you may want to name a style if your application contains multiple styles.

The following example defines a style called SystemResStyle that shows how to use the current system settings in your style. The example assigns the color of the ControlLightBrushKey property as the background color for the Button. It assigns the color of the ControlDarkBrushKey property as the foreground color for the Button. It also sets the characteristics of the Button text to SystemFonts characteristics.

You can use system metrics as either static or dynamic resources. Use a dynamic resource if you want the metric to automatically update while the application runs; otherwise, use a static resource.

<Style x:Key="SystemResStyle" TargetType="{x:Type Button}">
    <Setter Property = "Background" Value= 
                       "{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"/>
    <Setter Property = "Foreground" Value= 
                       "{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
    <Setter Property = "FontSize" Value= 
                       "{DynamicResource {x:Static SystemFonts.IconFontSizeKey}}"/>
    <Setter Property = "FontWeight" Value= 
                       "{DynamicResource {x:Static SystemFonts.MessageFontWeightKey}}"/>
    <Setter Property = "FontFamily" Value= 
                       "{DynamicResource {x:Static SystemFonts.CaptionFontFamilyKey}}"/>
</Style>

This example uses dynamic resources. Notice that dynamic resources have the keyword Key appended to the end of the property. For more information about how to use static resources, see How to: Use SystemParameters.

For the complete sample, see Button Styles Sample.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft