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
release/11.0.7
Max Katz 2 years ago
parent
commit
a0ab5c573d
  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); }
}
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));
}
}
}
/// <summary>
@ -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);
/// <summary>

20
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;

Loading…
Cancel
Save