0

I have a listview that's bound to a ListCollectionView. The LCV has a single sortdescription at any time. I am updating the collection in this manner:

IEditableCollectionView IEditView = lvBatches.Items as IEditableCollectionView;

IEditView.EditItem(m_collectionView.CurrentItem);//I've also tried passing MyListView.SelectedItem
((TestData)IEditView.CurrentEditItem).start = frm.newDate;
((TestData)IEditView.CurrentEditItem).edited = true;
IEditView.CommitEdit();

However, when I do, nothing happens to the listview's items. If I re-sort the list, the changes are then reflected. A Refresh() on the collection also updates the listview, but that's like using a stick of dynamite to open a soda can, from what I gather.

Does anyone have any ideas. My above code looks like the examples I'm seeing around the 'net so I don't think that's the problem. Are there any common mistakes people make anyone is aware of, maybe something to do with the sorting? I had it working and now it's not and I have no idea what broke it.

Thanks in advance.

Yatrix
  • 13,361
  • 16
  • 48
  • 78

2 Answers2

3

See my answer about creating a VeryObservableCollection.

The problem you are experiencing is that collections do not update with mere property changes -- CollectionChanged only fires if you add or remove elements. So you need to hook PropertyChanged and send a CollectionChanged when a property changes, which is what VeryObservableCollection does.

Community
  • 1
  • 1
Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • But I'm not using an ObserverableCollection to begin with, I'm using a ListCollectionView which should work when used with IEditableCollectionView as I have. I'd have to rewrite a bunch of stuff to handle the different collection. The thing is, I HAD this working and something broke it and I have no idea what did. By using iEditableCollectionView, it should allow me to EditItem and then CommitEdit which should tell the listview to update, right? – Yatrix Jul 29 '11 at 20:55
  • To be using a ListCollectionView you need to have a bound collection in there somewhere, right? Not sure about EditItem and what it does. – Ed Bayiates Jul 29 '11 at 21:01
  • @Jamison Yates, AresAvatar is correct in saying that the SOURCECOLLECTION of your CollectionView must be an ObservableCollection. – WPF-it Jul 30 '11 at 06:04
  • I'm confused. ListCollectionView takes an IList as it's only parameter. How do I have an ObserverableCollection as its source collection? The SourceCollection property is readonly. I tried using an ObserveableCollection bound to my listview to begin with, bu (ListViewItem)MyListView.ItemContainerGenerator.ContainerFromIndex(index) would always return null for some reason after update. Any code examples would be great that you can share on how to either use an ObsCollection as the source to a ListCView or how to get the item back from the ItemContainerGenerator after the collection is updated. – Yatrix Aug 01 '11 at 13:57
  • @Jamison: when you set up the ListView, what is its Source? That is the collection you want to work with, not the ListCollectionView. – Ed Bayiates Aug 01 '11 at 15:32
  • @AresAvatar I created a List and used that to create the LCV which I've been using as the source. MyListview.ItemsSource = collectionView; – Yatrix Aug 01 '11 at 15:37
  • Using List is the problem. If you change your List to an ObservableCollection or better, something like my VeryObservableCollection this issue should go away. You should not need to use the ListCollectionView this way. – Ed Bayiates Aug 01 '11 at 15:38
  • ObservableCollection goes back to my original problem though. I was using one but when I tried to grab the listviewitem associated with it, it was coming back null. I need to grab the item to change the style of it when it's been changed. I also liked the ease of sorting/filter with the listcollectionview - it suits my purposes well in that regard. I'm not sure how easy that is to accomplish with an ObserveableCollection. I was hoping to make what I had work as opposed to having to rewrite most (all) of it. Thanks a ton, btw. – Yatrix Aug 01 '11 at 15:52
  • You should be using the ListCollectionView for sorting. You just need to use the Source collection to tell the view that the collection has updated. If you want to continue to use Refresh you can do that, it's just that you have to do so manually and have no automated way like the CollectionChanged event. – Ed Bayiates Aug 01 '11 at 15:59
  • I did as you suggested. I set a property that was returning the collection's view. However, I still needed for the UI to reflect these changes which I found by implementing the iNotifyPropertyChanged. I appreciate all your help man. If you're ever in Youngstown, OH, I owe you a beer. =) – Yatrix Aug 01 '11 at 19:20
1

Are you calling NotifyPropertyChanged? If a Refesh() shows the correct values then most likely they are in the collection but the UI has to know to update value.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Haha. I was just about to post this as the answer. Damn - you get the point! – Yatrix Aug 01 '11 at 19:18
  • This is the link, for those of you who need it: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx – Yatrix Aug 01 '11 at 19:24