Click here to Skip to main content
16,020,622 members
Articles / Desktop Programming / WPF
Tip/Trick

Change Tracking an ObservableCollection

Rate me:
Please Sign up or sign in to vote.
1.95/5 (6 votes)
6 Jul 2018CPOL2 min read 10.8K   208   4   7
How to track changes in the ObservableCollection displayed in a WPF DataGrid

Image 1

Introduction

Tracking the changes (create, update, delete) in an ObservableCollection so that they can be saved to a disconnected backing store is not directly supported by the collection, and the solution is not immediately obvious. This can be an issue in multitier applications. Presented here is a class to handle the tracking, providing three lists of changes: RowsCreated, RowsUpdated, and RowsDeleted.

This is demonstrated in a small sample application which displays an ObservableCollection in a WPF DataGrid control. As the user adds, updates, and deletes rows, the ChangeTrack class tracks the changes, and presents the three lists in three additional DataGrid controls when the user clicks the ShowChanges button.

The ChangeTrack class does additional logic to minimize overlapping changes to the backing store. For example, if a row is added, but then deleted before saving to the backing store, the row is eliminated from both lists (RowsCreated, RowsDeleted), since no change to the backing store is needed in such a case. The unit tests exercise all seven combinations of create, update, and delete.

Using the code

The ChangeTrack class is the heart of this solution. Once the ObservableCollection is initially loaded, it is passed to an instance of ChangeTrack, which commences tracking any changes. The class exposes three lists, RowsCreated, RowsUpdated, and RowsDeleted, as public properties. As the collection is changed, these lists are kept up to date. After the changes are committed to the backing store, the program should call the ClearTracking method to clear the lists. At this point, any new changes will continue to be tracked. When the change tracking is no longer needed, the Dispose() method should be invoked to release the event handlers.

The setup is simple. The creation of the ChangeTrack instance requires the same Type of the row class held by the ObservableCollection. In the example, the row class is named "Data":

 ObservableCollection<Data> Rows;
ChangeTrack<Data> changeTrack = new ChangeTrack<Data>();

After the ObservableCollection is initially loaded, pass it to the StartTracking method of the ChangeTrack class to start tracking changes.

changeTrack.StartTracking(Rows);

There is also a HasChanges property that can be used to signal, for example, a RoutedUICommand, that is true when any changes have occurred.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Principal Software Engineer in medical device and related fields.

Comments and Discussions

 
PraiseNice Done you saved my day is the best Pin
Phantom Fox GX29-Nov-20 2:11
Phantom Fox GX29-Nov-20 2:11 
AnswerWhat's the point - response Pin
Daniel Ziegelmiller7-Jul-18 4:12
professionalDaniel Ziegelmiller7-Jul-18 4:12 
GeneralRe: What's the point - response Pin
Patrick Blackman7-Jul-18 5:30
professionalPatrick Blackman7-Jul-18 5:30 
Hi Daniel where can I find your Change tracking class, if you don't mind. Thanks in advance.

AnswerRe: What's the point - response Pin
Daniel Ziegelmiller7-Jul-18 6:17
professionalDaniel Ziegelmiller7-Jul-18 6:17 
Question[My vote of 1] What's The Point?? Pin
Kevin Marois6-Jul-18 11:15
professionalKevin Marois6-Jul-18 11:15 
QuestionOther solution Pin
LOST_FREEMAN6-Jul-18 10:21
LOST_FREEMAN6-Jul-18 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.