问题
I using a separate project for my styles a and i have some line of codes like this in it:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Style x:Key="NazaninFont" TargetType="Control">
<Setter Property="FontFamily" Value="pack://application:,,,/Extra/Fonts/#IRNazanin"/>
</Style>
......
</ResourceDictionary>
My another styles (like control effects and...) work well when i use my style key in the element like this:
Style="{ms:MyStyleRef ResourceKey=MyStyleKey}"
But When i use the following code in my Lable element
<Label Style="{ms:MyStyleRef ResourceKey=NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
I have:
- In Design Time >> The
FontFamily
is set on IRNazanin on the properties panel but have not correct font view in the designer!
BUT
-
In Run Time >> The
FontFamily
is set on Tahoma (Window font)
Also i tested this way:
I added a style base on my font styles in the separate project, in top of my Window, like this:
<Style x:Key="NazaninFont" BasedOn="{ms:MyStyleRef ResourceKey=NazaninFont}" TargetType="Label"/>
Then i use it in my Lable normaly:
<Label Style="{StaticResource NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
Result is same as previous way:
In the Design Time
in the FontFamily
is on IRNazanin and have not correct font view in XAML designer! And in Run Time
it is on Tahoma
What do you think about my problem? I think think my styles can not give the font path to the Label
control correctly.
回答1:
This may or may not be what you are after, but I'll take a guess that it is :p
You can define a FontFamily Resource in your resource dictionary, or where ever you want really. Take note of how I have defined it in Window.Resources.
I have included the physical font file in my project inside a folder called 'fonts' (no need for packing), and referenced it using the Font name: value. This is found by double clicking the font file (where it displays a whole heap of demo text and the option to install). You do not use the name of the file itself, you use the Font name: value when referencing. Also, don't forget the # at the start of any font reference! :)
Then, call it like you would any other resource for font family
Both the labels will be a different font. Please note that the font change won't be visible during design time unfortunately, only during runtime.
Working code as a demonstration:
<Window x:Class="Tinker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500">
<Window.Resources>
<FontFamily x:Key="MyFont">fonts/#Roboto Thin</FontFamily>
</Window.Resources>
<StackPanel VerticalAlignment="Top">
<Label FontSize="36" Content="Helloooooo World!"/>
<Label FontSize="36" FontFamily="{StaticResource MyFont}" Content="Helloooooo World!"/>
</StackPanel>
</Window>
来源:https://stackoverflow.com/questions/28433465/wpf-font-style-not-working-in-runtime-and-dont-show-correct-view-of-my-font-in