Browse Source
Make `SelectionModel.SelectedItems` and `SelectionModel.SelectedIndexes` implement `INotifyCollectionChanged` so that they can be bound to. As well as implementing `INotifyCollectionChanged` on the collections, we also had to implement `IList` (see #8764) so refactored this out into a base class. For the sake of simplicity, these collections only raise `Reset` for any change: this is may need to be changed later but I'd rather follow the KISS principle for the moment until something more complex is proven necessary. Fixes #15497release/11.1.0-beta2
committed by
Max Katz
5 changed files with 114 additions and 18 deletions
@ -0,0 +1,58 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Collections.Specialized; |
|||
using System.Diagnostics.CodeAnalysis; |
|||
using Avalonia.Collections; |
|||
|
|||
namespace Avalonia.Controls.Selection; |
|||
|
|||
internal abstract class ReadOnlySelectionListBase<T> : IReadOnlyList<T?>, IList, INotifyCollectionChanged |
|||
{ |
|||
public abstract T? this[int index] { get; } |
|||
public abstract int Count { get; } |
|||
|
|||
object? IList.this[int index] |
|||
{ |
|||
get => this[index]; |
|||
set => ThrowReadOnlyException(); |
|||
} |
|||
|
|||
bool IList.IsFixedSize => false; |
|||
bool IList.IsReadOnly => true; |
|||
bool ICollection.IsSynchronized => false; |
|||
object ICollection.SyncRoot => this; |
|||
|
|||
public event NotifyCollectionChangedEventHandler? CollectionChanged; |
|||
|
|||
public abstract IEnumerator<T?> GetEnumerator(); |
|||
public void RaiseCollectionReset() => CollectionChanged?.Invoke(this, EventArgsCache.ResetCollectionChanged); |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
|||
int IList.Add(object? value) { ThrowReadOnlyException(); return 0; } |
|||
void IList.Clear() => ThrowReadOnlyException(); |
|||
void IList.Insert(int index, object? value) => ThrowReadOnlyException(); |
|||
void IList.Remove(object? value) => ThrowReadOnlyException(); |
|||
void IList.RemoveAt(int index) => ThrowReadOnlyException(); |
|||
bool IList.Contains(object? value) => Count != 0 && ((IList)this).IndexOf(value) != -1; |
|||
|
|||
void ICollection.CopyTo(Array array, int index) |
|||
{ |
|||
foreach (var item in this) |
|||
array.SetValue(item, index++); |
|||
} |
|||
|
|||
int IList.IndexOf(object? value) |
|||
{ |
|||
for (int i = 0; i < Count; i++) |
|||
{ |
|||
if (Equals(this[i], value)) |
|||
return i; |
|||
} |
|||
|
|||
return -1; |
|||
} |
|||
|
|||
[DoesNotReturn] |
|||
private static void ThrowReadOnlyException() => throw new NotSupportedException("Collection is read-only."); |
|||
} |
|||
Loading…
Reference in new issue