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

ItemsRepeater not updating correctly after remove items from ObservableList #26

Open
adleywd opened this issue Apr 9, 2024 · 3 comments

Comments

@adleywd
Copy link

adleywd commented Apr 9, 2024

Describe the bug

I have a personal app that uses ItemsRepeater to list multiple tv shows data. Before updating to Avalonia 11.0.10 everything was working fine regarding removing items from an observable list and the item updating in ItemsRepeater.
I'm also using CommunitToolkit.MVVM.

After the update to 11.0.10 the ItemsRepeater, apparently, loses context the item context and shows the content as it is not loaded.
When I resized the screen "refreshes" and everything item came back to normal.

This project run in windows and android, but the issue only happens in Windows.

So before came here I tried to replicate the issue, with a similar approach I was using a smaller sample project, but I couldn't replicate the issue.

To Reproduce

So the project where the error happens is: (it's kinda big, idk if would help) https://github.com/adleywd/WhatsNextSeries/tree/feature/MigrateToDotNetAvalonia/src/WhatsNextSeries

The sample I tried to reproduce it (but I couldn't yet)
https://github.com/adleywd/AvaloniaItemsRepeaterSample

Expected behavior

When an item is removed from an observable list, the ItemsRepeater should remove the item but keep the context of unmodified items.

Avalonia version
11.0.10

OS
Windows

Additional context
With the bug in Avalonia 11.0.10
After removing and item from the list, other Items go back to default value:
"Tv show name"
"Not available"

11 0 10

With Avalonia 11.0.7
11 0 7

@adleywd
Copy link
Author

adleywd commented Apr 13, 2024

So after more debugging sessions, I still could not find the issue, nor replicate it in a smaller project, but It seems to related to be using a MenuFlyout to remove items. When I try to remove a command directly from the Flyout or even invoke the code from a code behind the flyout, the bug happens. If I remove the item from any other source, another window by referring to the same RemoveCommand or even using the code behind as below, it works as expected.

While debugging I checked that could be related to the RecyclePool in ItemTemplateWrapper.cs but I'm not sure since I did not dig that much into it.

<ItemsRepeater x:Name="FavoritesItemRepeater" ItemsSource="{Binding FavoritesShows}" >
                    <ItemsRepeater.Layout>
                        <UniformGridLayout ItemsJustification="Start"
                                           ItemsStretch="Fill"
                                           MaximumRowsOrColumns="4"
                                           MinColumnSpacing="5"
                                           MinRowSpacing="2"
                                           MinItemHeight="120"
                                           MinItemWidth="500" />
                    </ItemsRepeater.Layout>
                    <ItemsRepeater.Transitions>
                    </ItemsRepeater.Transitions>
                    <ItemsRepeater.ItemTemplate>
                        <DataTemplate DataType="viewModels:TvShowViewModel">
                            <Border>
                                <Border.ContextFlyout>
                                    <MenuFlyout >
                                        <MenuItem Header="{Binding TvShow.Name, StringFormat='Open {0}'}"
                                                  Command="{Binding OpenShowDetailsCommand}">
                                            <MenuItem.Icon>
                                                <avalonia:MaterialIcon Kind="OpenInNew"
                                                                       Foreground="{DynamicResource ColorPrimary}" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="{Binding TvShow.Name, StringFormat='Remove {0}'}"
                                                            Command="{Binding RemoveFavoriteCommand}">
                                            <MenuItem.Icon>
                                                <avalonia:MaterialIcon Kind="Delete"
                                                                       Foreground="{DynamicResource ColorDelete}" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </MenuFlyout>
                                </Border.ContextFlyout>
                                <components:FavoriteTvShowCard 
                                                              PointerPressed="InputElement_OnPointerPressed"
                                                               TvShowName="{Binding TvShow.Name}"
                                                               TvShowNextEpisode="{Binding NextEpisode}" />
                            </Border>
                        </DataTemplate>
                    </ItemsRepeater.ItemTemplate>
                </ItemsRepeater>
private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
    {
        if (sender is not Control control)
        {
            return;
        }

        if (control.DataContext is TvShowViewModel tvShowViewModel)
        {
            if (e.GetCurrentPoint(sender as Control).Properties.IsLeftButtonPressed)
            {
               tvShowViewModel.RemoveFavoriteCommand.Execute(null);
            }
        }
    }

@adleywd
Copy link
Author

adleywd commented Apr 13, 2024

I know that ItemsRepeater will eventually be discontinued, so I tested using ItemControl and everything seems to work fine, even better than ItemsRepeater, the only thing that holds me back is the missing options that UniforGridLayout.

Are there any plans to have more options in UniformGrid or something that will eventually replace the UniformGridLayout that Item Repeaters have?

@maxkatz6
Copy link
Member

@adleywd yes, there are plans to add VirtualizedUniformGrid to ItemsControl, but not in priority right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants