Why is TextBlock.FontSize converted into an attached property in binary (BAML)?

62 Views Asked by At

This XAML source:

<TextBlock Margin="10,0,0,0" FontSize="16" />

is compiled to BAML, according to ILSpy:

<TextBlock Margin="10,0,0,0" TextBlock.FontSize="16" />

I may be wrong but it seems the BAML version uses the attached property syntax instead of the property syntax (used for example for Margin).

I found:

which may be somehow contradictory with MSDN:

  • This dependency property also has an attached property usage. In XAML, the usage is <object TextBlock.FontSize="value".../>, where object is an object element (typically a flow element) contained within a TextBlock [...]

What is the correct explanation for this syntax difference between Margin and FontSize assignments in BAML?

1

There are 1 best solutions below

0
On

Take a look at the reference source. There is:

public static readonly DependencyProperty FontSizeProperty =
    TextElement.FontSizeProperty.AddOwner(typeof(TextBlock));

public double FontSize
{
    get { return (double)GetValue(FontSizeProperty); }
    set { SetValue(FontSizeProperty, value); }
}

with TextElement.FontSizeProperty defined like this:

public static readonly DependencyProperty FontSizeProperty =
    DependencyProperty.RegisterAttached(
        "FontSize",
        typeof(double),
        typeof(TextElement),
        new FrameworkPropertyMetadata(
            SystemFonts.MessageFontSize,
            FrameworkPropertyMetadataOptions.AffectsMeasure |
            FrameworkPropertyMetadataOptions.AffectsRender |
            FrameworkPropertyMetadataOptions.Inherits),
        new ValidateValueCallback(IsValidFontSize));

So while FontSize is not supposed to be used as an attached property, its identifier field is initialized by calling AddOwner on the identifier field of an attached property.

Just my guess, but I think that from this aspect TextBlock.FontSize is treated as an attached property.