Skip to content
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

Open
xLEGiON opened this issue Feb 26, 2024 · 1 comment · May be fixed by #266
Open

TreeDataGridPresenterBase Replace Regression #265

xLEGiON opened this issue Feb 26, 2024 · 1 comment · May be fixed by #266

Comments

@xLEGiON
Copy link

xLEGiON commented Feb 26, 2024

Pull request #241 ported some fixes from AvaloniaUI/Avalonia#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 xLEGiON linked a pull request Feb 26, 2024 that will close this issue
@xLEGiON
Copy link
Author

xLEGiON commented Dec 16, 2024

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:

Replace.Issue.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant