Avalonia.Controls.TreeDataGrid icon indicating copy to clipboard operation
Avalonia.Controls.TreeDataGrid copied to clipboard

TreeDataGridPresenterBase Replace Regression

Open xLEGiON opened this issue 1 year ago • 1 comments

Pull request https://github.com/AvaloniaUI/Avalonia.Controls.TreeDataGrid/pull/241 ported some fixes from https://github.com/AvaloniaUI/Avalonia/pull/13795 to RealizedStackElements, including the new ItemsReplaced method. It also made use of this more efficient method in TreeDataGridPresenterBase when the items collection changed with a NotifyCollectionChangedAction.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 to TreeDataGridPresenterBase, 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

xLEGiON avatar Feb 26 '24 14:02 xLEGiON

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>

ExampleCollection contains 500 string items with values "0" to "499" by default. When the "Test" button is clicked, items "5" and "6" are replaced with items: "new1", "new2", and "new3". However, the TreeDataGrid only displays them being replaced by items "new1" and "new2". Scrolling the items out of view and back into view corrects the display. Here is a video demonstration:

https://github.com/user-attachments/assets/1a39c8fa-cea1-433f-93a5-8756e3693662

xLEGiON avatar Dec 16 '24 14:12 xLEGiON