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

DataGrid does not refresh if source updated outside view #9527

Closed
GundlackFlorian opened this issue Nov 23, 2022 · 9 comments · Fixed by #12009
Closed

DataGrid does not refresh if source updated outside view #9527

GundlackFlorian opened this issue Nov 23, 2022 · 9 comments · Fixed by #12009
Labels
area-datagrid bug help-wanted A contribution from the community would be most welcome.

Comments

@GundlackFlorian
Copy link

GundlackFlorian commented Nov 23, 2022

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.
@enisn
Copy link

enisn commented Dec 20, 2022

After switching the tab, DataGrid even won't listen to PropertyChanged events anymore.

I tried to invoke property changed event for ItemsSource property in viewmodel while activating but it doesn't affect.

 this.WhenActivated(delegate (CompositeDisposable _)
  {
      // I expected it re-draws rows here but nothing happens.
      this.RaisePropertyChanged(nameof(Items));
  });

@maxkatz6 maxkatz6 added help-wanted A contribution from the community would be most welcome. area-datagrid labels Dec 20, 2022
@almightyju
Copy link
Contributor

almightyju commented Jan 13, 2023

This is caused by this code in the DataGrid:

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
// When wired to INotifyCollectionChanged, the DataGrid will be cleaned up by GC
if (DataConnection.DataSource != null && DataConnection.EventsWired)
{
DataConnection.UnWireEvents(DataConnection.DataSource);
}
}

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 bbf3099 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

@enisn
Copy link

enisn commented Mar 1, 2023

Any update on this?

@timunie
Copy link
Contributor

timunie commented Mar 2, 2023

@enisn the issue has the help wanted label. So if you or anyone else want to file a PR to solve the issue, feel free to do. Also testing latest master again could be useful.

@alexandrehtrb
Copy link

alexandrehtrb commented 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.

@liugangnhm
Copy link

any update?

@liugangnhm
Copy link

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 solve my issue, just use a index bindind

                    CurrentTabIndex = 1;
                    UpdateDatasource
                    CurrentTabIndex = 0;

@almightyju
Copy link
Contributor

I just tested this on master and I can't seem to reproduce it so I'm inclined to say it's fixed. Has anyone tried rc-1.1?

@liugangnhm
Copy link

I just tested this on master and I can't seem to reproduce it so I'm inclined to say it's fixed. Has anyone tried rc-1.1?

nothing changed at all, try the below demo
AvaloniaDataGridTest.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-datagrid bug help-wanted A contribution from the community would be most welcome.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants