|
|
@ -18,7 +18,7 @@ namespace Avalonia.Controls |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Displays a collection of items.
|
|
|
/// Displays a collection of items.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class ItemsControl : TemplatedControl, IItemsPresenterHost |
|
|
public class ItemsControl : TemplatedControl, IItemsPresenterHost, ICollectionChangedListener |
|
|
{ |
|
|
{ |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// The default value for the <see cref="ItemsPanel"/> property.
|
|
|
/// The default value for the <see cref="ItemsPanel"/> property.
|
|
|
@ -53,7 +53,6 @@ namespace Avalonia.Controls |
|
|
private IEnumerable _items = new AvaloniaList<object>(); |
|
|
private IEnumerable _items = new AvaloniaList<object>(); |
|
|
private int _itemCount; |
|
|
private int _itemCount; |
|
|
private IItemContainerGenerator _itemContainerGenerator; |
|
|
private IItemContainerGenerator _itemContainerGenerator; |
|
|
private IDisposable _itemsCollectionChangedSubscription; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Initializes static members of the <see cref="ItemsControl"/> class.
|
|
|
/// Initializes static members of the <see cref="ItemsControl"/> class.
|
|
|
@ -150,6 +149,19 @@ namespace Avalonia.Controls |
|
|
ItemContainerGenerator.Clear(); |
|
|
ItemContainerGenerator.Clear(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICollectionChangedListener.PreChanged(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e) |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICollectionChangedListener.Changed(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e) |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ICollectionChangedListener.PostChanged(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e) |
|
|
|
|
|
{ |
|
|
|
|
|
ItemsCollectionChanged(sender, e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Gets the item at the specified index in a collection.
|
|
|
/// Gets the item at the specified index in a collection.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
@ -315,12 +327,14 @@ namespace Avalonia.Controls |
|
|
/// <param name="e">The event args.</param>
|
|
|
/// <param name="e">The event args.</param>
|
|
|
protected virtual void ItemsChanged(AvaloniaPropertyChangedEventArgs e) |
|
|
protected virtual void ItemsChanged(AvaloniaPropertyChangedEventArgs e) |
|
|
{ |
|
|
{ |
|
|
_itemsCollectionChangedSubscription?.Dispose(); |
|
|
|
|
|
_itemsCollectionChangedSubscription = null; |
|
|
|
|
|
|
|
|
|
|
|
var oldValue = e.OldValue as IEnumerable; |
|
|
var oldValue = e.OldValue as IEnumerable; |
|
|
var newValue = e.NewValue as IEnumerable; |
|
|
var newValue = e.NewValue as IEnumerable; |
|
|
|
|
|
|
|
|
|
|
|
if (oldValue is INotifyCollectionChanged incc) |
|
|
|
|
|
{ |
|
|
|
|
|
CollectionChangedEventManager.Instance.RemoveListener(incc, this); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
UpdateItemCount(); |
|
|
UpdateItemCount(); |
|
|
RemoveControlItemsFromLogicalChildren(oldValue); |
|
|
RemoveControlItemsFromLogicalChildren(oldValue); |
|
|
AddControlItemsToLogicalChildren(newValue); |
|
|
AddControlItemsToLogicalChildren(newValue); |
|
|
@ -418,11 +432,9 @@ namespace Avalonia.Controls |
|
|
PseudoClasses.Set(":empty", items == null || items.Count() == 0); |
|
|
PseudoClasses.Set(":empty", items == null || items.Count() == 0); |
|
|
PseudoClasses.Set(":singleitem", items != null && items.Count() == 1); |
|
|
PseudoClasses.Set(":singleitem", items != null && items.Count() == 1); |
|
|
|
|
|
|
|
|
var incc = items as INotifyCollectionChanged; |
|
|
if (items is INotifyCollectionChanged incc) |
|
|
|
|
|
|
|
|
if (incc != null) |
|
|
|
|
|
{ |
|
|
{ |
|
|
_itemsCollectionChangedSubscription = incc.WeakSubscribe(ItemsCollectionChanged); |
|
|
CollectionChangedEventManager.Instance.AddListener(incc, this); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|