2012-04-19 31 views
0

在App.xaml中定义TextBox的FontFamily后,没有任何更改,这有点奇怪。代码是将FontFamily定义为Windows Phone中的资源

<Style TargetType="TextBox"> 
     <Setter Property="FontFamily" Value="Fonts/WenQuanYiMicroHei.ttf#WenQuanYiMicroHei"/> 
    </Style> 

当将TargetType更改为“TextBlock”时,TextBlock将更改其fontfamily。

此外,我还定义了一个样式像TextBock:

<Style x:Key="TextBoxStyle4FF" TargetType="TextBox"> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="BorderBrush" Value="White"/> 
     <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/> 
     <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="2"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="CaretBrush" Value="White"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Disabled"> 
           </VisualState> 
           <VisualState x:Name="ReadOnly"> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="0"> 
          <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="0" VerticalContentAlignment="Stretch" FontFamily="{TemplateBinding FontFamily}"/> 
         </Border> 
         <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed"> 
          <TextBox x:Name="DisabledOrReadonlyContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}" SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}" Template="{StaticResource PhoneDisabledTextBoxTemplate}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

是因为该文本框的样式定义?

所以我决定为TextBox定义一个FontFamily资源,但是找不到定义FontFamily的方法。似乎FontFamily位于System.Windows.Media名称空间中,但添加此名称空间xmlns:media="clr-namespace:System.Windows.Media;assembly=System.Windows"是无用的。

任何答复表示赞赏。

回答

0

你必须立足于已经定义的样式新样式:

<Style TargetType="TextBox" x:Key="baseStyle> 
    <Setter Property="FontFamily" 
      Value="Fonts/WenQuanYiMicroHei.ttf#WenQuanYiMicroHei"/> 
</Style> 

<Style x:Key="TextBoxStyle4FF" TargetType="TextBox" 
     BasedOn={StaticResource baseStyle}> <!-- based on defined style --> 

正如你所看到的,你必须给原有的风格的关键这样做。

+0

感谢您的回复。但是我必须根据用户选择的主题设置将这两种样式(样式和FontFamily)定义在两个不同的XAML文件中。所以baseStyle在Style XAML文件中是不可见的。 – ellic 2012-04-20 03:01:22

1

基于此模式:

<FontFamily x:Key="CantarellFontFamily" >Fonts/Cantarell-Regular.ttf#Cantarell</FontFamily> 

我创造了这样一条:

<FontFamily x:Key="CantarellFontFamily" >Fonts/Cantarell-Regular.ttf#Cantarell</FontFamily> 

,我在下面的方式为我工作在Windows Phone 7.1:

<TextBlock FontFamily="{StaticResource CantarellFontFamily}" /> 

你可以检查文档http://msdn.microsoft.com/en-us/library/system.windows.media.fontfamily(v=vs.95).aspx

Regards, Herber