I have an (external) model exposing a List that constantly changes (let's say every two seconds or so). A ViewModel knows that list registering for PropertyChange events. That ViewModel also provides an ObservableCollection to the UI for data binding.
+-----------------------------------------------+
| View|
| +-----------+ |
| |Listbox | |
| +-----------+ |
+-----/\----------------------------------------+
||
||DataBinding
||
||
+-----||----------------------------------------+
| || ViewModel|
| +--------------------+ +-------------+|
| |ObservableCollection|<--------|ChangeHandler||
| +--------------------+ / +-------------+|
| / ^ |
+-------------------------/------------|--------+
/ |
/ |
Synchronizing Lists | PropertyChanged
|
|
+--------------------------------------|--------+
| +-----+ Model|
| |IList| |
| +-----+ |
| |
+-----------------------------------------------+
In principle that works well, besides the updates conducted constantly. With every update the user looses his selection, i.e. all items will be deselected with every update. This is no wonder, as WPF's ListBox "sees" that a new list was assigned.
So, the thing must be that we do not assign a new ObservableCollection, but merge the content of the current ObservableCollection with the updated Model.List
Now my questions
- Synchronizing Lists - Are there best practices (or frameworks) on how to do such a merge (Copy new items to ObservableCollection, Deleting missing ones, updating altered items)
- Selected Item - How can I assure that the ListBox keeps the currently selected item (besides the case that item was removed)