question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataGrid does not refresh if source updated outside view

See original GitHub issue

Describe the bug In Avalonia 0.11.0-Preview4 when updating (add or delete) the Items of a DataGrid when the view is not displayed (in another TabItem for example) the DataGrid doesn’t get refreshed.

The issue doesn’t happen in 0.18.0

To Reproduce Steps to reproduce the behavior:

public class TestModel : ViewModelBase
{
    public string Name { get; }

    public TestModel( string name )
    {
        Name = name;
    }
}

public class MainWindowViewModel : ViewModelBase
{
    private ObservableCollection<TestModel> _items = new();
    public ObservableCollection<TestModel> Items
    {
        get => _items;
        set => this.RaiseAndSetIfChanged( ref _items, value );
    }

    public void Clear()
    {
        Items.Clear();
    }

    public void AddItems()
    {
        for ( int ii = 0; ii < 10; ii++ )
        {
            Items.Add( new TestModel( $"Item: {ii}" ) );
        }
    }
}
    <Window.DataTemplates>
        <DataTemplate DataType="vm:TestModel">
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </Window.DataTemplates>

    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Command="{Binding Clear}" Content="Clear" />
            <Button Command="{Binding AddItems}" Content="Add Items" />
        </StackPanel>

        <TabControl>
            <TabItem Header="A">
                <Grid ColumnDefinitions="*,*">
                    <DataGrid Grid.Column="0" Items="{Binding Items}">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:DataType="vm:TestModel" Header="Name" Binding="{Binding Name}" />
                        </DataGrid.Columns>
                    </DataGrid>

                    <ListBox Grid.Column="1" Items="{Binding Items}" />
                </Grid>
            </TabItem>

            <TabItem Header="B">
            </TabItem>
        </TabControl>
    </DockPanel>
  1. Click the Add Item Button
  2. Switch to Tab “B”
  3. Click the Clear Button
  4. Switch to Tab “A”
  5. The Items in the DataGrid have not been cleared (the ListBox is here for reference).

Same issue when clicking Add Items in the “B” Tab, the added Items are not displayed in the DataGrid.

Expected behavior

The removed items should not be shown.

Desktop:

  • Windows 10 Pro 22H2
  • Tested with Version 0.11.0-Preview1 and Version 0.11.0-Preview4.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:8
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
alexandrehtrbcommented, Jun 7, 2023

I just faced this issue, here is my temporary solution: switch to the tab that shows the DataGrid, update its ItemsSource, then switch back to the tab that was visible before. This makes the DataGrid view to be updated.

2reactions
almightyjucommented, Jan 13, 2023

This is caused by this code in the DataGrid: https://github.com/AvaloniaUI/Avalonia/blob/7580b704dff69b648b1fe73ebd7071b0c20bd4f0/src/Avalonia.Controls.DataGrid/DataGrid.cs#L2110-L2118

Simply not unwiring events seems to work but I don’t know what the resolution should be while keeping the intended behaviour.

Looking at the commit https://github.com/AvaloniaUI/Avalonia/commit/bbf3099a8f9f482f851f6cfe497f22c8ca95bbcf indicates the problem was a ViewModel/Collection keeping the DataGrid from being garbage collected, but that strikes me as an intended thing, if the ViewModel isn’t being released then why would you expect the view for it to be released.

On second thought perhaps simply calling InitializeElements(false /*recycleRows*/); during OnAttachedToVisualTree is called would be the best approach

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the DataGrid not update when the ItemsSource ...
I have a generic list and I want to bind this collection to my datagrid data source every time an object is being...
Read more >
DataGridView doesn't refresh to display updated value ...
DevExpress Support Team: CLONED FROM T906790: DataGridView doesn't refresh to display updated value after underlying source changes on Doub.
Read more >
DataGridView doesn't refresh to display updated value ...
We have a situation where we change some of the tapped item's values on DoubleTap event, but the changes don't get reflected in...
Read more >
Why listbox and datagridview does not update
Hi, I am relatively new to C# and am learning WindowsFormApp at the moment. My questions are related to listbox and datagridview please....
Read more >
DataGrid refresh on Reload/LoadData
I have a grid that I refresh on a timer that updates the grid data data source. Only add and deleted records refresh....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found