Skip to content

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

Closed
@sinatrocious

Description

@sinatrocious

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):

  • OS: Windows 10/11 (2 PCs with similar issue).
  • Version: Avalonia 11.0.0.

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:

  • make DataGrid a part of visual tree first, e.g. by first showing Popup and then adding items;
  • refresh DataGrid.ItemsSource by setting it to null and then back...

The ListBox doesn't have this issue.

The issue occurs with AvaloniaList and ObservableCollection<T>.

Deleting items and resetting collection also pose a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions