2009-09-21 13 views
10

'daki uygulama için FontFamily ve FontSize'yi ayarlama App.xaml'deki uygulama için FontFamily ve FontSize'yi nasıl ayarlayabilirim?App.xaml

cevap

12

2008'den bu yana bir blog post by David Padbury buldum ve bu koddan nasıl değiştirilir. Temel olarak, değişikliklerinizi mevcut değerlere birleştiren meta veri özelliklerini geçersiz kılarsınız.

TextElement.FontFamilyProperty.OverrideMetadata(
typeof(TextElement), 
new FrameworkPropertyMetadata(
    new FontFamily("Comic Sans MS"))); 

TextBlock.FontFamilyProperty.OverrideMetadata(
typeof(TextBlock), 
new FrameworkPropertyMetadata(
    new FontFamily("Comic Sans MS"))); 

da iki şekilde XAML nasıl yapılacağını açıklayan bu MSDN forum post var.

1) Öncelikle Control sınıfa

<Style TargetType="{x:Type Control}"> 
    <Setter Property="FontFamily" Value="Constantia"/> 
</Style> 

için "global" bir stil tanımlayabilir ve daha sonra diğer denetimlere o uygulamak için BasedOn özelliğini kullanın.

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<StackPanel.Resources> 
    <Style TargetType="{x:Type Control}" x:Key="ControlStyle"> 
    <Setter Property="FontFamily" Value="Constantia"/> 
    </Style> 

    <Style TargetType="{x:Type Label}" x:Key="LabelStyle" BasedOn="{StaticResource ControlStyle}"> 
    <Setter Property="FontWeight" Value="Bold" /> 
    </Style> 
     <Style TargetType="{x:Type Button}" x:Key="ButtonStyle" BasedOn="{StaticResource ControlStyle}"> 
     <Setter Property="Background" Value="Blue"/> 
    </Style> 
</StackPanel.Resources> 

<Label Style="{StaticResource LabelStyle}">This is a Label</Label> 
<Button Style="{StaticResource ButtonStyle}">This is a Button</Button> 
</StackPanel> 

2) Sistem yazı tiplerini ayarlayabilirsiniz:

<FontFamily x:Key="{x:Static SystemFonts.MenuFontFamilyKey}">./#Segoe UI</FontFamily> 
<System:Double x:Key="{x:Static SystemFonts.MenuFontSizeKey}">11</System:Double> 
<FontWeight x:Key="{x:Static SystemFonts.MenuFontWeightKey}">Normal</FontWeight> 

muhtemelen bu tavsiye etmem rağmen.

3
<Application.Resources> 
    <Style x:Key="WindowStyle" TargetType="{x:Type Window}"> 
      <Setter Property="FontFamily" Value="PalatineLinoType" /> 
    </Style> 
</Application.Resources> 
İlgili konular