Skip to content

Latest commit

 

History

History
96 lines (77 loc) · 6.68 KB

contentpresenter.md

File metadata and controls

96 lines (77 loc) · 6.68 KB
-api-id -api-type
T:Microsoft.UI.Xaml.Controls.ContentPresenter
winrt class

Microsoft.UI.Xaml.Controls.ContentPresenter

-description

Displays the content of a ContentControl. Can also provide content presentation for non-controls. Provides a base class for specialized presenters such as ScrollContentPresenter.

-xaml-syntax

<ContentPresenter .../>

-remarks

Typically, you use the ContentPresenter directly within the ControlTemplate of a ContentControl to mark where the content to be presented appears.

A ContentPresenter is often used to apply characteristics to text content, which are set into a Content property using only a string for the text (or some indirect equivalent such as a Binding or a RESX resource). For this reason the properties of a ContentPresenter are similar to the properties of the TextElement class. (The TextElement class is a base class for several elements that aren't controls but are used to format the text that might appear in a control or layout container.)

A ContentPresenter can use a logic class to influence which template to use for templated data content at run-time. For more info, see the ContentTemplateSelector property.

If the ContentPresenter is in the ControlTemplate of a ContentControl, the ContentPresenter behavior will implicitly bind to the ContentTemplate and Content properties of the templated ContentControl.

Border properties

ContentPresenter defines border properties that let you draw a border around the ContentPresenter without using an additional Border element. The properties are ContentPresenter.BorderBrush, ContentPresenter.BorderThickness, ContentPresenter.CornerRadius, and ContentPresenter.Padding.

<ContentPresenter BorderBrush="Red" BorderThickness="2" CornerRadius="10" Padding="12"/>

ContentPresenter derived classes

ContentPresenter is the parent class for these classes:

-examples

This example shows a typical placement of a ContentPresenter object element as part of the ControlTemplate for a ContentControl. This example is a simplification of the Windows Runtime XAML default style for HyperlinkButton. One example VisualState is included to show how the ContentPresenter properties are changed based on state. The ContentPresenter is within a Border in the composition; it's common for content controls to have the ContentPresenter nested within one other parent element, and that parent element often template-binds some of the control properties that a ContentPresenter doesn't have. Note also how the ContentPresenter uses {TemplateBinding} markup extension to bind HorizontalAlignment / VerticalAlignment values to the control properties of where the template is applied.

    <!-- Default style for Windows.UI.Xaml.Controls.Primitives.HyperlinkButton -->
    <Style TargetType="HyperlinkButton">
...
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
...
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="HyperlinkButton">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
...
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                       Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource HyperlinkDisabledThemeBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Border"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Margin="3">
                            <ContentPresenter x:Name="ContentPresenter"
                                              Content="{TemplateBinding Content}"
                                              ContentTransitions="{TemplateBinding ContentTransitions}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    />
                        </Border>
                        <!--focus visuals omitted-->
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

-see-also

ContentControl, FrameworkElement, ItemsPresenter, XAML Control templates