-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TreeDataGridPresenterBase Replace Regression #265
Comments
Here is a minimal reproducible example of the issue: using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using ReactiveUI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace Example;
public class MainViewModel : ReactiveObject
{
private readonly ExampleCollection _collection = new();
public MainViewModel()
{
Source = new FlatTreeDataGridSource<string>(_collection)
{
Columns =
{
new TextColumn<string, string>("Value", x => x),
},
};
}
public FlatTreeDataGridSource<string> Source { get; }
public void Test()
{
_collection.ReplaceRange(5, 2, ["new1", "new2", "new3"]);
}
}
public class ExampleCollection : IEnumerable<string>, IList, INotifyCollectionChanged
{
private readonly List<string> _items = new(Enumerable.Range(0, 500).Select(item => item.ToString()));
public event NotifyCollectionChangedEventHandler? CollectionChanged;
public object? this[int index] { get => _items[index]; set => _items[index] = (string)value!; }
public int Count => _items.Count;
public void ReplaceRange(int index, int count, string[] newItems)
{
string[] oldItems = _items.Skip(index).Take(count).ToArray();
_items.RemoveRange(index, count);
_items.InsertRange(index, newItems);
CollectionChanged?.Invoke(this, new(NotifyCollectionChangedAction.Replace, newItems, oldItems, index));
}
// not needed for example
public bool IsFixedSize => false;
public bool IsReadOnly => false;
public bool IsSynchronized => false;
public object SyncRoot => null!;
public int Add(object? value) => throw new NotImplementedException();
public void Clear() => throw new NotImplementedException();
public bool Contains(object? value) => throw new NotImplementedException();
public void CopyTo(Array array, int index) => throw new NotImplementedException();
public IEnumerator GetEnumerator() => throw new NotImplementedException();
public int IndexOf(object? value) => throw new NotImplementedException();
public void Insert(int index, object? value) => throw new NotImplementedException();
public void Remove(object? value) => throw new NotImplementedException();
public void RemoveAt(int index) => throw new NotImplementedException();
IEnumerator<string> IEnumerable<string>.GetEnumerator() => throw new NotImplementedException();
} <Grid Margin="50" ColumnDefinitions="*" RowDefinitions="*,Auto">
<TreeDataGrid BorderThickness="1" BorderBrush="White" Source="{Binding Source}"/>
<Button Grid.Row="1" Margin="0,6,0,0" Content="Test" Command="{Binding Test}" />
</Grid>
Replace.Issue.mp4 |
Pull request #241 ported some fixes from AvaloniaUI/Avalonia#13795 to
RealizedStackElements
, including the newItemsReplaced
method. It also made use of this more efficient method inTreeDataGridPresenterBase
when the items collection changed with aNotifyCollectionChangedAction.Replace
action.However, this made the assumption that the old and new items of the
NotifyCollectionChangedEventArgs
have the same length. When this is not true (e.g. replacing 2 items with 1 or 3 items), the TreeDataGrid does not display the collection change correctly. Before this change toTreeDataGridPresenterBase
, the collection change was displayed correctly.To fix this, it is still possible to use the more efficient
ItemsReplaced
method when the old and new items have the same length, but use the previous behavior when they do not. I've tested this idea and it fixes the issue. I will create a pull request with the proposed change.Environment:
TreeDataGrid Version: 11.0.2
Avalonia Version: 11.0.6
Operating System: Windows 10
The text was updated successfully, but these errors were encountered: