Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

When binding a Object that implements INotifyCollectionChanged to a ItemsControl, update the object doesn't trigger view updating #15607

Closed
cdytoby opened this issue May 4, 2024 · 5 comments

Comments

@cdytoby
Copy link

cdytoby commented May 4, 2024

Describe the bug

  • In ItemsControl, ItemsSource is binded to a custom object that implments INotifyCollectionChanged, IEnumerable and IReadOnlyCollection
  • that custom object gets updated, and CollectionChanged event is fired
  • In debugger, the values of the custom object are all correct
  • Bug: The actual ItemsControl view is empty

To Reproduce

DumbRenamer.zip

This is my project (just started not long ago, to experiment things)

Just build and run the project: DumbRenamer.AvaloniaApp.Desktop

In file MainViewModel.cs, line 64, in the method FileInputViewOnCollectionChanged(), you can put a break point there, and this break point gets triggered.

Expected behavior

The ItemsControl displays the items correctly

Avalonia version

11.0.10

OS

Windows

Additional context

I need a better ObservableCollection, so I found a third party one, they have their own solutions and I'd like to use it.
https://github.com/Cysharp/ObservableCollections

To my understanding of MVVM, as long as INotifyCollectionChanged is correctly fired, view should be updated. (experience from WPF)

(I could use Avalonia ones but I don't want avalonia package involved when design view model. And the Avalonia.Collection namespace is in the Avalonia main package, it's not a independent package.)

@cdytoby cdytoby added the bug label May 4, 2024
@stevemonaco
Copy link
Contributor

INCC needs to be on the UI thread. Using their ObservableList<T> works fine. It's only an issue with the INotifyCollectionChangedSynchronizedView<T>. Their WPF sample shows using BindingOperations.EnableCollectionSynchronization, so not being on the UI thread seems to be a possible scenario. I would have expected an exception or log message on Verbose, but I don't seem to get either.

@cdytoby
Copy link
Author

cdytoby commented May 4, 2024

I saw it, but I don't need to synchronize across thread (at least not right now), so I thought this is optional. ObservableList<T> don't have INCC, so INotifyCollectionChangedSynchronizedView<T> is the only option here if I want to keep using this.

@timunie
Copy link
Contributor

timunie commented May 5, 2024

Can you please file a minimum sample? I'll not explore a random file renamer as that could be harmful to my machine.

@cdytoby
Copy link
Author

cdytoby commented May 5, 2024

Hi, this name "DumbRenamer" is a project I just started, despite the solution name, there is not much code, and not much going on, that's why I upload it, and it's without any dll or built binary.

But I understand your concern. Here are the some sample code files in a zip (.cs file and .axaml files are not directly allowed somehow)

Code.zip

PS: CommunityToolkit.Mvvm package and ObservableCollections package from "https://github.com/Cysharp/ObservableCollections" are required.

@timunie
Copy link
Contributor

timunie commented May 6, 2024

You need the Dispather if you update things from a Task.

private async Task ChangeValue()
{
    await Task.Delay(1000);
    Dispatcher.UiThread.Post(() => { TestInt = 10 });
    ExecutePreview();
}

@timunie timunie removed the bug label May 6, 2024
@AvaloniaUI AvaloniaUI locked and limited conversation to collaborators May 6, 2024
@timunie timunie converted this issue into discussion #15623 May 6, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants