How to set FontSize in pt if we use StaticResource or DynamicResource?
currently i am using fontsize resource as
<sys:Double x:Key="FontSize13">13</sys:Double>
<sys:Double x:Key="FontSize12">12</sys:Double>
<sys:Double x:Key="FontSize11">11</sys:Double>
and using as
<Setter Property="FontSize"
Value="{DynamicResource FontSize13}" />
How to set the Fon开发者_运维技巧tSize in point like 10pt instead of pixel?
The type conversion happens at compile time by the XAML compiler and specifically in response to the FontSizeConverter
being present for the FontSize
property so we have a basic problem getting the converter to run. But we can create a helper markup extension to do the job.
Here's what the XAML looks like:
<Grid>
<Grid.Resources>
<local:FontSize Size="20" x:Key="TwentyPixels"/>
<local:FontSize Size="11pt" x:Key="ElevenPoint"/>
</Grid.Resources>
<StackPanel>
<TextBlock Text="Sample text" FontSize="{StaticResource TwentyPixels}"/>
<TextBlock Text="Sample text" FontSize="{StaticResource ElevenPoint}"/>
</StackPanel>
</Grid>
and here's the markup extension:
public class FontSizeExtension : MarkupExtension
{
[TypeConverter(typeof(FontSizeConverter))]
public double Size { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Size;
}
}
Just use a space between number and "pt". For example:
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontSize" Value="11 pt"/>
</Style>
Just changed the resource from a Double to a String and include the unit specifier
<sys:String x:Key="FontSize13">13pt</sys:String>
精彩评论