|
|
@ -1,6 +1,8 @@ |
|
|
|
|
|
using System; |
|
|
using Avalonia.Collections; |
|
|
using Avalonia.Collections; |
|
|
using Avalonia.Controls.Presenters; |
|
|
using Avalonia.Controls.Presenters; |
|
|
using Avalonia.Controls.Templates; |
|
|
using Avalonia.Controls.Templates; |
|
|
|
|
|
using Avalonia.Data; |
|
|
using Avalonia.LogicalTree; |
|
|
using Avalonia.LogicalTree; |
|
|
|
|
|
|
|
|
namespace Avalonia.Controls.Primitives |
|
|
namespace Avalonia.Controls.Primitives |
|
|
@ -10,6 +12,9 @@ namespace Avalonia.Controls.Primitives |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class HeaderedItemsControl : ItemsControl, IContentPresenterHost |
|
|
public class HeaderedItemsControl : ItemsControl, IContentPresenterHost |
|
|
{ |
|
|
{ |
|
|
|
|
|
private IDisposable? _itemsBinding; |
|
|
|
|
|
private bool _prepareItemContainerOnAttach; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Defines the <see cref="Header"/> property.
|
|
|
/// Defines the <see cref="Header"/> property.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
@ -60,6 +65,17 @@ namespace Avalonia.Controls.Primitives |
|
|
/// <inheritdoc/>
|
|
|
/// <inheritdoc/>
|
|
|
IAvaloniaList<ILogical> IContentPresenterHost.LogicalChildren => LogicalChildren; |
|
|
IAvaloniaList<ILogical> IContentPresenterHost.LogicalChildren => LogicalChildren; |
|
|
|
|
|
|
|
|
|
|
|
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) |
|
|
|
|
|
{ |
|
|
|
|
|
base.OnAttachedToLogicalTree(e); |
|
|
|
|
|
|
|
|
|
|
|
if (_prepareItemContainerOnAttach) |
|
|
|
|
|
{ |
|
|
|
|
|
PrepareItemContainer(); |
|
|
|
|
|
_prepareItemContainerOnAttach = false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
/// <inheritdoc/>
|
|
|
bool IContentPresenterHost.RegisterContentPresenter(IContentPresenter presenter) |
|
|
bool IContentPresenterHost.RegisterContentPresenter(IContentPresenter presenter) |
|
|
{ |
|
|
{ |
|
|
@ -81,6 +97,37 @@ namespace Avalonia.Controls.Primitives |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
internal void PrepareItemContainer() |
|
|
|
|
|
{ |
|
|
|
|
|
_itemsBinding?.Dispose(); |
|
|
|
|
|
_itemsBinding = null; |
|
|
|
|
|
|
|
|
|
|
|
var item = Header; |
|
|
|
|
|
|
|
|
|
|
|
if (item is null) |
|
|
|
|
|
{ |
|
|
|
|
|
_prepareItemContainerOnAttach = false; |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var headerTemplate = HeaderTemplate; |
|
|
|
|
|
|
|
|
|
|
|
if (headerTemplate is null) |
|
|
|
|
|
{ |
|
|
|
|
|
if (((ILogical)this).IsAttachedToLogicalTree) |
|
|
|
|
|
headerTemplate = this.FindDataTemplate(item); |
|
|
|
|
|
else |
|
|
|
|
|
_prepareItemContainerOnAttach = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (headerTemplate is ITreeDataTemplate treeTemplate && |
|
|
|
|
|
treeTemplate.Match(item) && |
|
|
|
|
|
treeTemplate.ItemsSelector(item) is { } itemsBinding) |
|
|
|
|
|
{ |
|
|
|
|
|
_itemsBinding = BindingOperations.Apply(this, ItemsProperty, itemsBinding, null); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private void HeaderChanged(AvaloniaPropertyChangedEventArgs e) |
|
|
private void HeaderChanged(AvaloniaPropertyChangedEventArgs e) |
|
|
{ |
|
|
{ |
|
|
if (e.OldValue is ILogical oldChild) |
|
|
if (e.OldValue is ILogical oldChild) |
|
|
|