-1

I have a DataGrid bound via a ListCollectionView to an ObservableCollection of Objects with type Job, say. Each cell in the DataGrid contains a UserControl which has a dependency property named Job which is bound to the DataGridRow.DataContext (using Mode=TwoWay). Everything displays correctly.

The problem is that I have a background process which mutates objects referenced by the Job object and those get displayed by the UserControl. Obviously, Job does not change so the view does not change. How can I get the user controls in each cell to update themselves with the new data?

1 Answers1

0

With lists there are 3 kinds of ChangeNotification you have to take care of:

  • the one for each property of the Job Class.
  • the one if elements are added or removed from the collection. That is the only part ObservableCollection takes care off.
  • the one on the property exposing the list/CollectionView/whatever. ObservableCollection lacks a AddRange function so on large operations (like a rebuild of the list) it would spam the UI with changes. The solution is to build a new list in the background, then expose it in this property.

One particular issue might be the Background Process too, if it is Multithreading. GUI's are protected against being written from the wrong Thread. But threads are also notoriously good at swallowing Exceptions. Usually you need to write a faulty Catch, but they do it for free. As a result, your might run int oa CrossThread exception and never notice it.

For a better answer, we need some actuall code. Like teh job class, the BackgroundProcess, etc.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • The background thread uses Dispatcher.BeginInvoke to update the UI data objects. I think the problem is that the user control has only the Job binding and this does not change. I am going to try to add another property to the Job object called revision number and bind this to a new Dependency property on the user control. The updater can then increase the revision number which should trigger a property change in the user control which can then update itself. I will re-post here when I have a result. – Peter Wilson Oct 25 '19 at 14:57
  • @PeterWilson What about the 3 forms of binding? Why do you not show us code? And did I read it right that you used DependencyProperties for your ViewModel? – Christopher Oct 25 '19 at 14:59
  • The technique I outlined above works. However, we have hit multiple problems with the background updater: 1. Massive Server hit, 2. Stupifying memory leak, 3. SQL deadlocks. We have decided to go back to a simple refresh button. This also solves the problem as we refresh the whole grid. – Peter Wilson Oct 25 '19 at 18:26
  • @PeterWilson: For 2, People often missread the memory figures or missunderstand how the GC works: https://stackoverflow.com/a/58486473/3346583 | Without showing us the querry we can not really help with it. – Christopher Oct 25 '19 at 18:33