baozi
baozi

Reputation: 709

How to use ObservableCollection databinding?

Hello all i am pretty new to c# mvvm or data binding. I saw some examples using ObservableCollection As explanined, An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions.

I have the following code: a property of type ObservableCollection<StudentViewModel> but it also implements the INotifyPropertyChanged interface. So why it needs to implement the INotifyPropertyChanged interface with type ObservableCollection here ? why not just automatic notification?

public ObservableCollection<StudentViewModel> TheStudents
    {
        get
        {
            return _theStudents;
        }

        set
        {
            if (_theStudents == value)
                return;

            if (_theStudents != null)
            {
                foreach (var StudentViewModel in _theStudents)
                {
                    DisconnectStudentViewModel(StudentViewModel);
                }
            }

            _theStudents = value;

            if (_theStudents != null)
            {
                foreach (var StudentViewModel in _theStudents)
                {
                    ConnectStudentViewModel(StudentViewModel);
                }
            }

            OnPropertyChanged("TheStudents");
        }
    }

more background: simply to say when click button, the following function GetStudentsAction() will trigger------>TheStudents = sth in turn should told the VIEW that property has changed.

public void GetStudentsAction()
    {
        TheStudents = GetStudentsDelegate();

        IsSaveStudentsActionEnabled = true;
        IsAddStudentsActionEnabled = true;
    }

Upvotes: 1

Views: 65

Answers (1)

Michael Edenfield
Michael Edenfield

Reputation: 28338

The ObservableCollection class sends notifications when the contents of the collection change, such as adding or removing an item. These are special "this collection has changed" events that WPF can listen for (as defined in INotifyCollectionChanged).

It does not send notifications when you swap out the collection itself for a new instance of the collection. That's the responsibility of the object that has the collection as a property. This is the same event you have to send whenever any property on your view model has changed (as defined in INotifyPropertyChanged).

As a rule of thumb, it's rarely a good idea to have collection properties that can be set externally, the way you're using them. Typically you would give your collection property a private setter, and only allow external classes to add, remove, and clear the items from it. That eliminates the need to have a notification if your collection changes: you instantiate it once, in the constructor, and it never changes after that.

(That advice may or may not apply to your situation, but typically when I see collection types with public setters, it indicates a flaw in the design.)

Upvotes: 1

Related Questions