// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Controls.Mixins; using Avalonia.Controls.Presenters; using Avalonia.Controls.Templates; using Avalonia.LogicalTree; namespace Avalonia.Controls.Primitives { /// /// A with a header. /// public class HeaderedContentControl : ContentControl, IHeadered { /// /// Defines the property. /// public static readonly StyledProperty HeaderProperty = AvaloniaProperty.Register(nameof(Header)); /// /// Defines the property. /// public static readonly StyledProperty HeaderTemplateProperty = AvaloniaProperty.Register(nameof(HeaderTemplate)); /// /// Initializes static members of the class. /// static HeaderedContentControl() { ContentProperty.Changed.AddClassHandler((x, e) => x.HeaderChanged(e)); } /// /// Gets or sets the header content. /// public object Header { get { return GetValue(HeaderProperty); } set { SetValue(HeaderProperty, value); } } /// /// Gets the header presenter from the control's template. /// public IContentPresenter HeaderPresenter { get; private set; } /// /// Gets or sets the data template used to display the header content of the control. /// public IDataTemplate HeaderTemplate { get { return GetValue(HeaderTemplateProperty); } set { SetValue(HeaderTemplateProperty, value); } } /// protected override bool RegisterContentPresenter(IContentPresenter presenter) { var result = base.RegisterContentPresenter(presenter); if (presenter.Name == "PART_HeaderPresenter") { HeaderPresenter = presenter; result = true; } return result; } private void HeaderChanged(AvaloniaPropertyChangedEventArgs e) { if (e.OldValue is ILogical oldChild) { LogicalChildren.Remove(oldChild); } if (e.NewValue is ILogical newChild) { LogicalChildren.Add(newChild); } } } }