2

I am using an ObservableCollection for databinding as ItemsSource for DataGrid. Collection contains complex type objects. One of this type properties is a List of strings.

Just for now I see that when I update this List property from code nothing changes in the UI (the primary binding works fine). So, my question is: is it an expected behaviour? Maybe I should not use List as part of the type, but also use an ObservableCollection?

Update

Mode is set to OneWay.

26071986
  • 2,320
  • 3
  • 23
  • 34
  • Jehof is correct, however, you may also need to use a HierarchicalDataTemplate (depending on view). – Danny Varod Jun 21 '11 at 07:43
  • Type should expose ICollection or another interface as property, backing field should implement INotifyCollectionChanged, such as ObservableCollection. – Danny Varod Jun 21 '11 at 07:47

4 Answers4

2

Use a collection, instead of List, that implementes the interface INotifyCollectionChanged (like ObservableCollection). Then changes to the collection get populated to the ui.

Jehof
  • 34,674
  • 10
  • 123
  • 155
0

When you are updateding your list u have to call INotifyPropertyChange other wise UI wont get update the list result..

INotifyPropertyChange is the indication that here some changes occurred in the items source so update it.

Syeda
  • 1,215
  • 11
  • 23
  • Won't help, since comparison with old value will be the same. Also the external collection did not change - no need to update its view (performance) inner collection is the one that must implement **INotifyCollectionChanged**. – Danny Varod Jun 21 '11 at 07:39
  • @Dammy: INotifyCollectionChanged will work no doubt.. But i worked with property using INotifyCollectionChanged for binding it works for me. The way i knew i told.. – Syeda Jun 21 '11 at 08:04
0

This might help as well:

ObservableCollection that also monitors changes on the elements in collection

Community
  • 1
  • 1
Marc
  • 12,706
  • 7
  • 61
  • 97
0

Yes it is expected behaviour. The observable collection only notifies of changes to its own contents - that is add, delete, reorder.

What you are looking at is a change to an element in the observablecollection - if you want to see your changes to the class you put in, your element has to implement INotifyPropertyChanged.

So currently: If your list property on you complex object changes you won't see it, however if you change that too to be an observablecollection you could see changes to that collection in a sub-itemscontrol like a combobox - but not if you change the collection object to another one - so if you do not implement INotifyPropertyChanged you should set the collectionproperty before the binding is applied.

Rune Andersen
  • 1,650
  • 12
  • 15
  • That's just what I've done an hour earlier - ObservableCollection instead of List + INotifyPropertyObject on the whole structure (shame on me, I should have thought of it earlier). I mark you answer as a solution to my problem. – 26071986 Jun 21 '11 at 10:33