Defining the Default Content Property

,

The Content property is the default property of the ContentControl. The ContentPropertyAttribute specifies which property of a class can be interpreted as the main content property when the class is parsed by a XAML processor. This means that when, for example, a TextBlock is placed as a nested element of a Button, the TextBlock is rendered within the button itself:

<Button>
    <TextBlock Text="Press me!" Foreground="Orange" />
</Button>

To further illustrate, the built-in control template for the Button control includes a Border that contains a TemplateBinding to the Content property of the Button, as shown in the following excerpt:

<Border>
    <ContentControl x:Name="ContentContainer"
                    ContentTemplate="{TemplateBinding ContentTemplate}"
                    Content="{TemplateBinding Content}" />
</Border>

When the Button is rendered using its built-in style, the Content value, whatever it may be, is placed within a Border.

If you examine the source code for the ContentControl using Reflector, you see that a ContentPropertyAttribute is used to indicate to the runtime that the Content property is the default property:

[ContentProperty("Content", true)]
public class ContentControl : Control
{ ... }

When creating your own custom controls, you can decorate a class with a ContentPropertyAttribute to specify a different default property.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.8.35