Compartilhar via


Como: Usar SystemFonts

This example shows how to use the static resources of the SystemFonts class in order to style or customize a button.

Exemplo

Os recursos de sistema exponham vários valores de determinadas pelo sistema como propriedades e recursos para ajudá-lo a criar elementos visuais que são consistentes com as configurações do sistema. SystemFontsé uma classe que contém os dois valores de fonte de sistema como propriedades estáticas e propriedades que referenciam chaves de recurso que podem ser usadas para acessar esses valores dinamicamente em tempo de execução. For example, CaptionFontFamily is a SystemFonts value, and CaptionFontFamilyKey is a corresponding resource key.

Em XAML, você pode usar os membros do SystemFonts como propriedades estáticas ou referências de recurso dinâmico (com o valor da propriedade estática como chave). Use a dynamic resource reference if you want the font metric to automatically update while the application runs; otherwise, use a static value reference.

Observação

The resource keys have the suffix "Key" appended to the property name.

The following example shows how to access and use the properties of SystemFonts as static values in order to style or customize a button. This markup example assigns SystemFonts values to a button.

<Button Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="3"      
     FontSize="{x:Static SystemFonts.IconFontSize}"
     FontWeight="{x:Static SystemFonts.MessageFontWeight}"
     FontFamily="{x:Static SystemFonts.CaptionFontFamily}">
     SystemFonts
</Button>

To use the values of SystemFonts in code, you do not 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.

Dim btn As New Button()
btn.Content = "SystemFonts"
btn.Background = SystemColors.ControlDarkDarkBrush
btn.FontSize = SystemFonts.IconFontSize
btn.FontWeight = SystemFonts.MessageFontWeight
btn.FontFamily = SystemFonts.CaptionFontFamily
cv1.Children.Add(btn)
Button btncsharp = new Button();
btncsharp.Content = "SystemFonts";
btncsharp.Background = SystemColors.ControlDarkDarkBrush;
btncsharp.FontSize = SystemFonts.IconFontSize;
btncsharp.FontWeight = SystemFonts.MessageFontWeight;
btncsharp.FontFamily = SystemFonts.CaptionFontFamily;
cv1.Children.Add(btncsharp);

Consulte também

Tarefas

Como: Paint an Area with a System Brush

Como: Usar SystemParameters

Como: Use System Fonts Keys

Referência

SystemFonts

Extensão de Marcação x:Static

Extensão de marcação DynamicResource

Conceitos

Visão geral sobre Recursos

Outros recursos

Tópicos de Como fazer Recursos