-2

I'm working in WPF MVVM application in which i have a scenario where i need to clear a list which is binded as two way to a radGrid (i.e) have to show the grid empty on click of a button.

I tried using ,

SeriesSearchList.Clear(); //Does not work.

SeriesSearchList = null; // Works.

I have declared in this way,

private List<SeriesSearchBO> m_lSearchList;

 public List<string> SeriesSearchList
   {
      get { return this.m_lSearchList; }
      set
          {
            if (this.m_lSearchList!= value)
              {
                 this.m_lSearchList= value;
                 OnPropertyChanged();
              }
          }
   }

Just curious why am i not able to have the List.Clear() working with two way binding.

A Coder
  • 3,039
  • 7
  • 58
  • 129
  • 9
    Use ObservableCollection instead of List – General-Doomer May 08 '15 at 08:19
  • "SeriesSearchList = null " assigning new value, OnPropertyChanged is invoked "SeriesSearchList.Clear()" old list reference, OnPropertyChanged not invoking You should use ObservableCollection or after clear invoke method OnPropertyChanged("SeriesSearchList") – Pawel Maga May 08 '15 at 08:22
  • Related [here](http://stackoverflow.com/questions/3807665/onpropertychanged-with-a-list), [here](http://stackoverflow.com/questions/956165/wpf-onpropertychanged-for-a-property-within-a-collection), this question has been asked *several* times before. – Patrick May 08 '15 at 08:49

2 Answers2

2

In the first case, you removing all elements from list, but does not assign a new value (old reference) In second case you assigning new value to list so method OnPropertyChanged is invoked.

You can solve this problem in two ways:

  1. Use ObservableCollection instead of List like said @General-Doomer
  2. SeriesSearchList.Clear(); OnPropertyChanged("SeriesSearchList");
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
-1

As General-Doomer pointed out, you need to use an ObservableCollection instead of a standard List

The reason for this is because ObservableCollection publishes an event every time the content of the underlying list changes. And the WPF framework hooks on this, and will automatically update the UI whenever it's posted.

Now the reason that Clear doesn't work, but setting it to Null DOES, is because when you Clear it, you're changing the CONTENT of the property, whereas setting it to null changes the ACTUAL property. The UI is bound to the SeriesList property, and when this is changed (set to null), the UI will update to reflect it.

So in short, use ObservableCollection if you wish for all changes (In the collection. Not on the objects in the collection) to automatically get updates on the view.

Falgantil
  • 1,290
  • 12
  • 27
  • You are wrong when you say "ObservableCollection publishes an event every time the content of the underlying list changes.". `ObservableCollection` only publish a change event when the reference of the list itself change. Content changes are not notified. If you want that behavior you can use `System.ComponentModel.BindingList`. See http://stackoverflow.com/a/4284805/4668080 if you want to know the difference. @Bjarke Søgaard – Guerudo May 08 '15 at 08:38
  • When I say that the content of the collection changes, I don't mean the properties on the items in the collection. I mean the actual quantity of the collection. I thought that was clear. – Falgantil May 08 '15 at 08:45