using Avalonia.Collections; using Avalonia.Controls.Metadata; using Avalonia.Controls.Mixins; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Layout; using Avalonia.LogicalTree; using Avalonia.Metadata; namespace Avalonia.Controls { /// /// Displays according to an . /// [TemplatePart("PART_ContentPresenter", typeof(ContentPresenter))] public class ContentControl : TemplatedControl, IContentControl, IContentPresenterHost { /// /// Defines the property. /// public static readonly StyledProperty ContentProperty = AvaloniaProperty.Register(nameof(Content)); /// /// Defines the property. /// public static readonly StyledProperty ContentTemplateProperty = AvaloniaProperty.Register(nameof(ContentTemplate)); /// /// Defines the property. /// public static readonly StyledProperty HorizontalContentAlignmentProperty = AvaloniaProperty.Register(nameof(HorizontalContentAlignment)); /// /// Defines the property. /// public static readonly StyledProperty VerticalContentAlignmentProperty = AvaloniaProperty.Register(nameof(VerticalContentAlignment)); /// /// Gets or sets the content to display. /// [Content] [DependsOn(nameof(ContentTemplate))] public object? Content { get { return GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } } /// /// Gets or sets the data template used to display the content of the control. /// public IDataTemplate? ContentTemplate { get { return GetValue(ContentTemplateProperty); } set { SetValue(ContentTemplateProperty, value); } } /// /// Gets the presenter from the control's template. /// public ContentPresenter? Presenter { get; private set; } /// /// Gets or sets the horizontal alignment of the content within the control. /// public HorizontalAlignment HorizontalContentAlignment { get { return GetValue(HorizontalContentAlignmentProperty); } set { SetValue(HorizontalContentAlignmentProperty, value); } } /// /// Gets or sets the vertical alignment of the content within the control. /// public VerticalAlignment VerticalContentAlignment { get { return GetValue(VerticalContentAlignmentProperty); } set { SetValue(VerticalContentAlignmentProperty, value); } } /// IAvaloniaList IContentPresenterHost.LogicalChildren => LogicalChildren; /// bool IContentPresenterHost.RegisterContentPresenter(ContentPresenter presenter) { return RegisterContentPresenter(presenter); } protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); if (change.Property == ContentProperty) { ContentChanged(change); } } /// /// Called when an is registered with the control. /// /// The presenter. protected virtual bool RegisterContentPresenter(ContentPresenter presenter) { if (presenter.Name == "PART_ContentPresenter") { Presenter = presenter; return true; } return false; } private void ContentChanged(AvaloniaPropertyChangedEventArgs e) { if (e.OldValue is ILogical oldChild) { LogicalChildren.Remove(oldChild); } if (e.NewValue is ILogical newChild) { LogicalChildren.Add(newChild); } } } }