DataGrid does not refresh if source updated outside view
See original GitHub issueDescribe 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>
- Click the
Add Item
Button - Switch to Tab “B”
- Click the
Clear
Button - Switch to Tab “A”
- 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:
- Created 10 months ago
- Reactions:8
- Comments:9 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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*/);
duringOnAttachedToVisualTree
is called would be the best approach