Browse Source

Added IChildIndexProvider.TotalCountChanged.

pull/10055/head
Steven Kirk 4 years ago
parent
commit
f289099aeb
  1. 7
      src/Avalonia.Base/LogicalTree/IChildIndexProvider.cs
  2. 19
      src/Avalonia.Base/Styling/Activators/NthChildActivator.cs
  3. 8
      src/Avalonia.Controls/ItemsControl.cs
  4. 15
      src/Avalonia.Controls/Panel.cs

7
src/Avalonia.Base/LogicalTree/IChildIndexProvider.cs

@ -25,8 +25,13 @@ namespace Avalonia.LogicalTree
bool TryGetTotalCount(out int count);
/// <summary>
/// Notifies subscriber when child's index or total count was changed.
/// Notifies subscriber when a child's index was changed.
/// </summary>
event EventHandler<ChildIndexChangedEventArgs>? ChildIndexChanged;
/// <summary>
/// Notifies subscriber when the total child count changes.
/// </summary>
event EventHandler<EventArgs>? TotalCountChanged;
}
}

19
src/Avalonia.Base/Styling/Activators/NthChildActivator.cs

@ -1,4 +1,5 @@
#nullable enable
using System;
using Avalonia.LogicalTree;
namespace Avalonia.Styling.Activators
@ -33,8 +34,16 @@ namespace Avalonia.Styling.Activators
return NthChildSelector.Evaluate(index, _provider, _step, _offset, _reversed).IsMatch;
}
protected override void Initialize() => _provider.ChildIndexChanged += ChildIndexChanged;
protected override void Deinitialize() => _provider.ChildIndexChanged -= ChildIndexChanged;
protected override void Initialize()
{
_provider.ChildIndexChanged += ChildIndexChanged;
_provider.TotalCountChanged += TotalCountChanged;
}
protected override void Deinitialize()
{
_provider.ChildIndexChanged -= ChildIndexChanged;
}
private void ChildIndexChanged(object? sender, ChildIndexChangedEventArgs e)
{
@ -47,5 +56,11 @@ namespace Avalonia.Styling.Activators
ReevaluateIsActive();
}
}
private void TotalCountChanged(object? sender, EventArgs e)
{
if (_reversed)
ReevaluateIsActive();
}
}
}

8
src/Avalonia.Controls/ItemsControl.cs

@ -88,6 +88,7 @@ namespace Avalonia.Controls
private int _itemCount;
private ItemContainerGenerator? _itemContainerGenerator;
private EventHandler<ChildIndexChangedEventArgs>? _childIndexChanged;
private EventHandler<EventArgs>? _totalCountChanged;
private IDataTemplate? _displayMemberItemTemplate;
private Tuple<int, Control>? _containerBeingPrepared;
private ScrollViewer? _scrollViewer;
@ -203,6 +204,12 @@ namespace Avalonia.Controls
remove => _childIndexChanged -= value;
}
event EventHandler<EventArgs>? IChildIndexProvider.TotalCountChanged
{
add => _totalCountChanged += value;
remove => _totalCountChanged -= value;
}
/// <summary>
/// Returns the container for the item at the specified index.
/// </summary>
@ -418,6 +425,7 @@ namespace Avalonia.Controls
else if (change.Property == ItemCountProperty)
{
UpdatePseudoClasses(change.GetNewValue<int>());
_totalCountChanged?.Invoke(this, EventArgs.Empty);
}
else if (change.Property == ItemContainerThemeProperty && _itemContainerGenerator is not null)
{

15
src/Avalonia.Controls/Panel.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Avalonia.LogicalTree;
using Avalonia.Media;
@ -34,6 +35,7 @@ namespace Avalonia.Controls
}
private EventHandler<ChildIndexChangedEventArgs>? _childIndexChanged;
private EventHandler<EventArgs>? _totalCountChanged;
/// <summary>
/// Initializes a new instance of the <see cref="Panel"/> class.
@ -41,6 +43,7 @@ namespace Avalonia.Controls
public Panel()
{
Children.CollectionChanged += ChildrenChanged;
Children.PropertyChanged += ChildrenPropertyChanged;
}
/// <summary>
@ -64,6 +67,12 @@ namespace Avalonia.Controls
remove => _childIndexChanged -= value;
}
event EventHandler<EventArgs>? IChildIndexProvider.TotalCountChanged
{
add => _totalCountChanged += value;
remove => _totalCountChanged -= value;
}
/// <summary>
/// Renders the visual to a <see cref="DrawingContext"/>.
/// </summary>
@ -161,6 +170,12 @@ namespace Avalonia.Controls
InvalidateMeasure();
}
private void ChildrenPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Children.Count) || e.PropertyName is null)
_totalCountChanged?.Invoke(this, EventArgs.Empty);
}
private static void AffectsParentArrangeInvalidate<TPanel>(AvaloniaPropertyChangedEventArgs e)
where TPanel : Panel
{

Loading…
Cancel
Save