-api-id | -api-type |
---|---|
T:Microsoft.UI.Xaml.Controls.ContentPresenter |
winrt class |
Displays the content of a ContentControl. Can also provide content presentation for non-controls. Provides a base class for specialized presenters such as ScrollContentPresenter.
<ContentPresenter .../>
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.
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 is the parent class for these classes:
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>
ContentControl, FrameworkElement, ItemsPresenter, XAML Control templates