![]() ![]() Today we learned the difference between list and ObservableCollection through an example. Listing 8: Actor class with BindableBase: INotifyPropert圜hangedĬhanging Age, In figure 8 you would see it has successfully updated the age of Meryl Streep and that has also been notified to UI, this is because of the Propert圜hanged event.įigure 7: Updating age of Meryl Streep with ObservableCollection and ListView is reflecting these changes Summary We are only updating the Age property for now, because in our UpdateActor() we are only updating the age of an actor. ![]() We are using the BindableBase class which implements INotifyPropert圜hanged interface. Code snippet 8 shows how to achieve this. You need to update the Actor class to reflect propertychanged events. Now let's update the age of Meryl Streep from 72 to 70. This is because ObservableCollection uses INotif圜ollectionChanged event to trigger the source that list has been modified.įigure 7: Adding new record with ObservableCollection and ListView has added a new record In figure 7, you can see as soon as new actor's details have been added to the list, The ListBox is automatically updated. Now let's run the project and add a few details. Note: there is one more reference in the LoadCollectionData() method, change that to ObservableCollection as well. Listing 7: ObservableCollection of Actors In ViewModel, we need to create List of Actors. We need Actor's class, following snippet is for class Actor internal class Actor Forget about the data that we are going to add from ViewModel next.įigure 2: WPF app to Add and Update collection Run the project, you'll see the following screen. Listing 1: XAML for the Screen in figure 2 This listing contains everything that is mentioned above. And inside a MainWindow adds the following code. Alright, having said that go ahead and create a WPF application. A small form to create a new actor object with 2 buttons, one to create and another to alter the actor's details. Our screen which is MainWindow will have a ListBox to show the details of actors. The object we are going to work with is Actor. This is a WPF application, following the MVVM pattern. This is good in theory, but we need to understand it practically. ![]() Well this is what List is missing, which makes ObservableCollection so special. In the above figure, there is event Propert圜hangedEventHandler, which is Propert圜hanged, this notifies the listener when value of the property has been changed. In figure 1 you can see there is Notif圜ollectionChangedEventHandler, this is CollectionChanged event which notifies the listener whenever an item has been added or removed from the list. If you look at the implementation of ObservableCollection, You would see that it inherits INotif圜ollectionChanged, INotifyPropert圜hanged as shown in figure 1.įigure 1: Class ObservableCollection INotif圜ollectionChanged So when we already have a List why do we need ObservableCollection? In nutshell both List and ObservableCollection inherits ICollection, IList interfaces. Today we will learn the subtle difference between ObservableCollection and List. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |