在App.xaml中为TextBox定义FontFamily后,没有任何改变,这有点奇怪。代码是
<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的样式定义吗?
所以我决定为TextBox定义一个FontFamily资源,但找不到定义FontFamily的方法。似乎FontFamily位于System.Windows.Media
命名空间中,但添加此命名空间xmlns:media="clr-namespace:System.Windows.Media;assembly=System.Windows"
是没用的。
感谢任何回复。
答案 0 :(得分: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
上的文档此致 数量锐减
答案 1 :(得分: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 -->
正如你所看到的,你必须给原始风格一个关键。