Редагувати

Поділитися через


How to use system resources (WPF .NET)

This example demonstrates how to use system-defined resources. System resources are provided by WPF and allow access to operating system resources, such as fonts, colors, and icons. System resources expose several system-defined values as both resources and properties to help you create visuals that are consistent with Windows.

Fonts

Use the SystemFonts class to reference the fonts used by the operating system. This class contains system font values as static properties, and properties that reference resource keys that can be used to access those values dynamically at run time. For example, CaptionFontFamily is a SystemFonts value, and CaptionFontFamilyKey is a corresponding resource key.

The following example shows how to access and use the properties of SystemFonts as static values to style or customize a text block:

<TextBlock FontSize="{x:Static SystemFonts.SmallCaptionFontSize}"
           FontWeight="{x:Static SystemFonts.SmallCaptionFontWeight}"
           FontFamily="{x:Static SystemFonts.SmallCaptionFontFamily}"
           Text="Small Caption Font">
</TextBlock>

To use the values of SystemFonts in code, you don't have to use either a static value or a dynamic resource reference. Instead, use the non-key properties of the SystemFonts class. Although the non-key properties are apparently defined as static properties, the run-time behavior of WPF as hosted by the system will reevaluate the properties in real time and will properly account for user-driven changes to system values. The following example shows how to specify the font settings of a button:

var myButton = new Button()
{
    Content = "SystemFonts",
    Background = SystemColors.ControlDarkDarkBrush,
    FontSize = SystemFonts.IconFontSize,
    FontWeight = SystemFonts.MessageFontWeight,
    FontFamily = SystemFonts.CaptionFontFamily
};

mainStackPanel.Children.Add(myButton);
Dim myButton = New Button() With
{
    .Content = "SystemFonts",
    .Background = SystemColors.ControlDarkDarkBrush,
    .FontSize = SystemFonts.IconFontSize,
    .FontWeight = SystemFonts.MessageFontWeight,
    .FontFamily = SystemFonts.CaptionFontFamily
}

mainStackPanel.Children.Add(myButton)

Dynamic fonts in XAML

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

Note

Dynamic resources have the keyword Key appended to the property name.

The following example shows how to access and use system font dynamic resources to style or customize a text block:

<TextBlock FontSize="{DynamicResource {x:Static SystemFonts.SmallCaptionFontSize}}"
           FontWeight="{DynamicResource {x:Static SystemFonts.SmallCaptionFontWeight}}"
           FontFamily="{DynamicResource {x:Static SystemFonts.SmallCaptionFontFamily}}"
           Text="Small Caption Font">
</TextBlock>

Parameters

Use the SystemParameters class to reference system-level properties, such as the size of the primary display. This class contains both system parameter value properties, and resource keys that bind to the values. For example, FullPrimaryScreenHeight is a SystemParameters property value and FullPrimaryScreenHeightKey is the corresponding resource key.

The following example shows how to access and use the static values of SystemParameters to style or customize a button. This markup example sizes a button by applying SystemParameters values to a button:

<Button FontSize="8" 
        Height="{x:Static SystemParameters.CaptionHeight}"
        Width="{x:Static SystemParameters.IconGridWidth}"
        Content="System Parameters">
</Button>

To use the values of SystemParameters in code, you don't have to use either static references or dynamic resource references. Instead, use the values of the SystemParameters class. Although the non-key properties are apparently defined as static properties, the run-time behavior of WPF as hosted by the system will reevaluate the properties in real time, and will properly account for user-driven changes to system values. The following example shows how to set the width and height of a button by using SystemParameters values:

var myButton = new Button()
{
    Content = "SystemParameters",
    FontSize = 8,
    Background = SystemColors.ControlDarkDarkBrush,
    Height = SystemParameters.CaptionHeight,
    Width = SystemParameters.CaptionWidth,
};

mainStackPanel.Children.Add(myButton);
Dim myButton = New Button() With
{
    .Content = "SystemParameters",
    .FontSize = 8,
    .Background = SystemColors.ControlDarkDarkBrush,
    .Height = SystemParameters.CaptionHeight,
    .Width = SystemParameters.CaptionWidth
}

mainStackPanel.Children.Add(myButton)

Dynamic parameters in XAML

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

Note

Dynamic resources have the keyword Key appended to the property name.

The following example shows how to access and use system parameter dynamic resources to style or customize a button. This XAML example sizes a button by assigning SystemParameters values to the button's width and height.

<Button FontSize="8" 
        Height="{DynamicResource {x:Static SystemParameters.CaptionHeightKey}}"
        Width="{DynamicResource {x:Static SystemParameters.IconGridWidthKey}}"
        Content="System Parameters">
</Button>

See also