0

有点奇怪的是,在 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 Resource,但是找不到定义FontFamily的方法。好像 FontFamily 是在System.Windows.Media命名空间中,但是添加这个命名空间 xmlns:media="clr-namespace:System.Windows.Media;assembly=System.Windows"是没有用的。

任何答复表示赞赏。

4

2 回答 2

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查看文档

问候,赫伯

于 2012-07-19T19:49:53.743 回答
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 -->

如您所见,您必须为原始样式提供密钥才能这样做。

于 2012-04-19T15:52:34.903 回答