diff --git a/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs b/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs index d21a7bdb3f..8934b08b98 100644 --- a/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs +++ b/src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs @@ -1153,15 +1153,23 @@ namespace Avalonia.Collections get { return GetItemAt(index); } } - bool IList.IsFixedSize => false; - bool IList.IsReadOnly => true; + bool IList.IsFixedSize => SourceList?.IsFixedSize ?? true; + bool IList.IsReadOnly => SourceList?.IsReadOnly ?? true; bool ICollection.IsSynchronized => false; object ICollection.SyncRoot => this; object IList.this[int index] { get => this[index]; - set => throw new NotSupportedException(); + set + { + SourceList[index] = value; + if (SourceList is not INotifyCollectionChanged) + { + // TODO: implement Replace + ProcessCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, value)); + } + } } /// @@ -3992,9 +4000,36 @@ namespace Avalonia.Collections } } - int IList.Add(object value) => throw new NotSupportedException(); - void IList.Clear() => throw new NotSupportedException(); - void IList.Insert(int index, object value) => throw new NotSupportedException(); + int IList.Add(object value) + { + var index = SourceList.Add(value); + if (SourceList is not INotifyCollectionChanged) + { + ProcessCollectionChanged( + new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value)); + } + return index; + } + + void IList.Clear() + { + SourceList.Clear(); + if (SourceList is not INotifyCollectionChanged) + { + ProcessCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } + } + + void IList.Insert(int index, object value) + { + SourceList.Insert(index, value); + if (SourceList is not INotifyCollectionChanged) + { + // TODO: implement Insert + ProcessCollectionChanged( + new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, value)); + } + } void ICollection.CopyTo(Array array, int index) => InternalList.CopyTo(array, index); /// diff --git a/src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs b/src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs index fc9aac0ab8..835bb566fe 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs @@ -349,15 +349,15 @@ namespace Avalonia.Controls { Debug.Assert(index >= 0); - IList list = List; - if (list != null) + if (DataSource is DataGridCollectionView collectionView) { - return (index < list.Count) ? list[index] : null; + return (index < collectionView.Count) ? collectionView.GetItemAt(index) : null; } - if (DataSource is DataGridCollectionView collectionView) + IList list = List; + if (list != null) { - return (index < collectionView.Count) ? collectionView.GetItemAt(index) : null; + return (index < list.Count) ? list[index] : null; } IEnumerable enumerable = DataSource; @@ -419,15 +419,15 @@ namespace Avalonia.Controls public int IndexOf(object dataItem) { - IList list = List; - if (list != null) + if (DataSource is DataGridCollectionView cv) { - return list.IndexOf(dataItem); + return cv.IndexOf(dataItem); } - if (DataSource is DataGridCollectionView cv) + IList list = List; + if (list != null) { - return cv.IndexOf(dataItem); + return list.IndexOf(dataItem); } IEnumerable enumerable = DataSource;