Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify Gui that data class has changed

Tags:

c#

In C#: I have a data class that is shared amongst several gui classes. I would like all of the gui classes that use it to be notified when some of the properties change, so they can keep the GUI up to date.

In a couple of the properties I have added delegates that the GUI classes can listen to for updates. This seems to work ok.

The problem I have is that more and more of the properties will require GUI notification. When that happens I will have to add more delegates. It also seems that this is adding an extra responsibility to the data class that it has to manage.

Is there some common pattern that I can use to monitor this class to extract this notification responsibility from the data class?

like image 745
MattR Avatar asked Dec 18 '22 03:12

MattR


1 Answers

The common way of doing this is for the data class to implement INotifyPropertyChanged.

EDIT: If you have a lot of properties, this can lead to very repetitive code in the data class, and if you are binding to a UI, it might be best to use an AOP approach and intercept calls to the properties that you want to notify on. Most IoC containers have support for this sort of thing.

like image 96
Lee Avatar answered Jan 14 '23 03:01

Lee