AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.34k stars 2.2k forks source link

`DataGrid` is desynced (showing wrong content) after updates done to its `ItemsSource` while not present in visual tree #12356

Closed sinatrocious closed 1 year ago

sinatrocious commented 1 year ago

Describe the bug If DataGrid is removed from visual tree then it will ignore changes done to collection bound to its ItemsSource. This happens, to example, when DataGrid is located inside an inactive TabItem or not yet opened Popup.

To Reproduce Here is repro: AvaloniaApplication1.zip

Or simply make a new Avalonia project and modify MainWindow:

Axaml:

<Grid>
    <TabControl>
        <TabItem Header="DataGrid">
            <DataGrid x:Name="dataGrid"
                ItemsSource="{Binding Items}"
                AutoGenerateColumns="True"/>
        </TabItem>
        <TabItem Header="Empty"/>
    </TabControl>
    <StackPanel HorizontalAlignment="Right">
        <Button Content="{Binding Items.Count}"
            Command="{Binding AddCommand}"/>
    </StackPanel>
</Grid>

ViewModel:

public class MainViewModel : ViewModelBase
{
    public AvaloniaList<string> Items { get; } = new();
    public ReactiveCommand<Unit, Unit> AddCommand { get; }
    public MainViewModel()
    {
        int counter = 0;
        AddCommand = ReactiveCommand.Create(() => Items.Add(counter++.ToString()));
    }
}

App.xaml:

    <Application.Styles>
        <StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
        ...

Here is a TabControl with 2 tabs: one with DataGrid and empty one. A button on the right displayes items count.

Switching to another tab and pressing button to add items will lead to the state, where DataGrid is desynchronized and displays wrong content, which does not correspond to the actual.

Expected behavior The DataGrid should show correct and actual content, disregards if items are added while it's not visible.

Screenshots

Gif: Animation

Desktop (please complete the following information):

Additional context

The issue arises when we start using Avalonia 11.0.0. Everything was ok in 0.10.19.

There are several workarounds, so it's not urgent to fix:

The ListBox doesn't have this issue.

The issue occurs with AvaloniaList and ObservableCollection<T>.

Deleting items and resetting collection also pose a problem.

sinatrocious commented 1 year ago

Will the issue get fixed with #12009?

timunie commented 1 year ago

@sinatrocious good question. The PR you mentioned is merged. this means, you can test with using nightly builds. You can close this issue if fixed in nightly builds.

https://github.com/AvaloniaUI/Avalonia/wiki/Using-nightly-build-feed

sinatrocious commented 1 year ago

@timunie , I've updated avalonia to 11.0.1, the issue is fixed.