Class ContentControl
A Control that hosts a single arbitrary UIElement as its content, decorated by the inherited Background/BorderBrush/BorderThickness.
[ContentProperty("Content")]
public class ContentControl : Control, INotifyPropertyChanging, IBindingTarget, INotifyPropertyChanged, INotifyFocusChanged, IContainerLayout
- Inheritance
-
ContentControl
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
When Template is set, the template's content is responsible for actually hosting
Content - typically via a ContentPresenter bound with
{TemplateBinding Content}. Content itself keeps working the same way either way; only
where it's stored (and who parents it) changes, mirroring Control's own dual-path
Background/etc.
Properties
Content
Gets or sets the element displayed as this control's content.
public UIElement? Content { get; set; }
Property Value
Methods
CaptureTemplateState()
Captures whatever subclass-specific state needs to survive a Template swap, beyond the four decoration properties this class already preserves on its own.
protected override object? CaptureTemplateState()
Returns
- object
Opaque state later passed back to RestoreTemplateState(object?).
Remarks
Returns null by default. ContentControl overrides this pair to also preserve Content across a swap. This is also the seed of what a future named-template-parts addition is expected to build on: it runs once per swap, at whichever of the two points RestoreTemplateState(object?)'s own remarks describe - exactly where discovering a new template's named parts would need to happen too.
GetHostedElement(object?)
Gets the element, if any, that the old Chrome displays but doesn't own, so a Template swap leaves its bindings alone.
protected override UIElement? GetHostedElement(object? state)
Parameters
stateobjectThe state CaptureTemplateState() returned before the swap.
Returns
Remarks
A swap unbinds every element of the old Chrome, so its {TemplateBinding}s stop
following this control. Elements the control only hosts there belong to the application, and so do their
{Binding}s. They move on to the new Chrome and must keep working.
ContentControl returns its Content.
RestoreTemplateState(object?)
Restores state captured by CaptureTemplateState() after a Template swap.
protected override void RestoreTemplateState(object? state)
Parameters
stateobjectThe state CaptureTemplateState() returned before the swap.
Remarks
Called exactly once per swap, at one of two different points depending on direction - check Template (already the new value by the time this runs) to tell which:
-
Switching to a template (Template is non-null): called
before the new template's content is built, so a
{TemplateBinding}inside it captures the restored value on its very first read. Restore state directly into a backing field here, never through a public property setter that participates in the value-precedence system (mirrors how this class's own Background/BorderBrush/BorderThickness/ Padding are restored - see Template's setter for why). - Switching away from a template (Template is null): called after Chrome has already been replaced by the fresh default Border - restore state into that Border's own properties directly; it's brand new, so there's no precedence-poisoning risk there.