Kishore Kumar
Kishore Kumar

Reputation: 21863

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 FontSize in point like 10pt instead of pixel?

Upvotes: 5

Views: 7785

Answers (3)

Allender
Allender

Reputation: 604

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>

Upvotes: -1

Rick Sladkey
Rick Sladkey

Reputation: 34250

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;
    }
}

Upvotes: 12

Ian Oakes
Ian Oakes

Reputation: 10253

Just changed the resource from a Double to a String and include the unit specifier

<sys:String x:Key="FontSize13">13pt</sys:String>

Upvotes: -3

Related Questions