Browse Source

Fix DataGrid IList editing issue (#13845)

* Implement basic writeable IList on DataGridCollectionView hopefully it won't break anything else

* Make preference over DataGridCollectionView in DataGridDataConnection
pull/13893/head
Max Katz 3 years ago
committed by GitHub
parent
commit
5625f548a9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs
  2. 20
      src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs

47
src/Avalonia.Controls.DataGrid/Collections/DataGridCollectionView.cs

@ -1153,15 +1153,23 @@ namespace Avalonia.Collections
get { return GetItemAt(index); } get { return GetItemAt(index); }
} }
bool IList.IsFixedSize => false; bool IList.IsFixedSize => SourceList?.IsFixedSize ?? true;
bool IList.IsReadOnly => true; bool IList.IsReadOnly => SourceList?.IsReadOnly ?? true;
bool ICollection.IsSynchronized => false; bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => this; object ICollection.SyncRoot => this;
object IList.this[int index] object IList.this[int index]
{ {
get => this[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));
}
}
} }
/// <summary> /// <summary>
@ -3992,9 +4000,36 @@ namespace Avalonia.Collections
} }
} }
int IList.Add(object value) => throw new NotSupportedException(); int IList.Add(object value)
void IList.Clear() => throw new NotSupportedException(); {
void IList.Insert(int index, object value) => throw new NotSupportedException(); 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); void ICollection.CopyTo(Array array, int index) => InternalList.CopyTo(array, index);
/// <summary> /// <summary>

20
src/Avalonia.Controls.DataGrid/DataGridDataConnection.cs

@ -349,15 +349,15 @@ namespace Avalonia.Controls
{ {
Debug.Assert(index >= 0); Debug.Assert(index >= 0);
IList list = List; if (DataSource is DataGridCollectionView collectionView)
if (list != null)
{ {
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; IEnumerable enumerable = DataSource;
@ -419,15 +419,15 @@ namespace Avalonia.Controls
public int IndexOf(object dataItem) public int IndexOf(object dataItem)
{ {
IList list = List; if (DataSource is DataGridCollectionView cv)
if (list != null)
{ {
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; IEnumerable enumerable = DataSource;

Loading…
Cancel
Save