Developing WPF classes for data binding

I have to build a chat application in WPF; I want to use DataBinding (still learning) and want to do it right. I built the Buddy class this way:

public class Buddy: INotifyPropertyChanged { private String _name; private String _status; public String Name { get { return _name; } set { _name = value; NotifyPropertyChanged("Name"); } } public String Status { get { return _status; } set { _status = value; NotifyPropertyChanged("Status"); } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 

I do not know what is the best way to deal with BuddyList. Should I create a BuddyList class using the Add and List method and then DataBinding for an instance of this class? What is the best way to do this?

+4
source share
3 answers

You can use the INotifyCollectionChanged Interface to create your BuddyListClass
Here is an example: Using the INotifyCollectionChanged You can also use the ObservableCollection<T> Class .

+3
source

If you want to link collections of items, you must use the ObservableCollection class

and other sentences create a base class that implements the INotifyPropertyChanged interface, and extract from it each class that you want to bind to the user interface.

+2
source

I find that defining your class looks simple.

Regarding the question about the list, I would publish my list as a snap, and I would keep all the functions of adding, deleting and editing it in a private form. In order not to inform you manually about your view of the changes in your collection, I would use an ObservableCollection , but I would give it to the public as ReadOnlyObservableCollection .

+1
source

All Articles