How WPF defines font size in static resources

The unit of the default WPF font size is pixels. If you want to express the font size in pt points, write it in xaml to add the pt suffix directly. But at this time, if pt is written when the static resource is trying to be defined, it will prompt that it cannot be converted at runtime.

The default unit is Pixel as written in the following code

            <TextBlock Margin="10,10,10,10"
                       FontSize="10"
                       Text="林德熙是逗比"></TextBlock>
            <TextBlock Margin="10,10,10,10"
                       FontSize="10pt"
                       Text="林德熙是逗比"></TextBlock>

The actual running effect can be seen that the font using pt is obviously larger than the pixel

This is written in xaml, if you want to write in the resource, such as the following code, will not be able to run

    <Window.Resources>
        <system:String x:Key="FontSize">10pt</system:String>
    </Window.Resources>
    <Grid>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Margin="10,10,10,10"
                       FontSize="10"
                       Text="林德熙是逗比"></TextBlock>
            <TextBlock Margin="10,10,10,10"
                       FontSize="{StaticResource FontSize}"
                       Text="林德熙是逗比"></TextBlock>
        </StackPanel>
    </Grid>

The reason is that the FontSize class is a double type, and the build will prompt that the string cannot be converted to the double class

An object of the type "System.String" cannot be applied to a property that expects the type "System.Double".	CelakercalbochallhiNerjufeeqalchelfu	MainWindow.xaml	19	

But why is it possible to add the unit pt in the attribute written in xaml, because the attribute TypeConverter is marked in the FontSize attribute to convert through this

According to this method, a special font size class can be defined locally

using System.Windows.Markup;

public class FontSizeExtension : MarkupExtension
{
    [TypeConverter(typeof(FontSizeConverter))]
    public double Size { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Size;
    }
}

Put this class in the code, and then you can write the following code in the xaml resource

    <Window.Resources>
        <local:FontSize x:Key="FontSize" Size="10pt"></local:FontSize>
    </Window.Resources>
    <Grid>
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
            <TextBlock Margin="10,10,10,10"
                       FontSize="10"
                       Text="林德熙是逗比"></TextBlock>
            <TextBlock Margin="10,10,10,10"
                       FontSize="{StaticResource FontSize}"
                       Text="林德熙是逗比"></TextBlock>
        </StackPanel>
    </Grid>

In use MarkupExtension can ignore Extensionwrite only the first part, which is written FontSizein resources, in other words, have no problem writing FontSizeExtension

    <Window.Resources>
        <local:FontSizeExtension x:Key="FontSize" Size="10pt"></local:FontSizeExtension>
    </Window.Resources>

This allows you to define the font size in static resources

Knowledge Sharing License Agreement
This work is licensed under the Creative Commons Attribution-Non-Commercial Use-Share 4.0 International License Agreement in the same way . Reprint, use, and repost are welcome, but the article signature Lin Dexi (including link: http://blog.csdn.net/lindexi_gd ) must be retained , and may not be used for commercial purposes. The modified works based on this article must be released under the same license. If you have any questions, please feel free to contact .

Guess you like

Origin www.cnblogs.com/lindexi/p/12717510.html