I am trying to create List that can perform an action whenever I make a change to it.
I recently started learning how to use Blazor and I saw how reactive it was on the webpage.
So I decided to write my collection in a way that it emits an event and updates parts the page whenever I make a change to it. I started with List<T> before going on to ObservableCollection<T> . But with the ObservableCollection I can only emit events when I add or remove items. I want to be able to emit events when elements of the collection have been changed
7 Replies
I dont want to write hacks. Is there like a normal way to do this or its not done normally
There is no automatic way to handle "when an item in the list changes", as that doesnt involve the list itself
Okay, I understand
unfortunately there is no easy way to know whether some arbitrary item changed in an arbitrary way. But you can wrap your elements in a class that tell emit an event when some specific changes happen
can you be more specific about your items and what kind of changes you want to track?
Its fine. I did what you said. I wrapped my items in a class and attached the events to them individually. I then put the items into the ObservableCollection list and also attached the event to it. I had to overload the action method so it accepted both PropertyChangedEvents and NotifyCollectionChangedEvent
So now anytime I add or remove stuff, or even change a value. It emits the event and runs the action. I dont know if thats a hack or not. But it works
Sounds reasonable to me
Alright...💯