Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem which I cannot resolve although saw tens of samples with the issue.

and the problem is: changing an item within an ObservableCollection.
The only way I could see changes in the UI is by implementing the following
XML
ObservableCollection<Item> temp = new ObservableCollection<Item>();
foreach (Item item in Items) temp.Add(item);
Items.Clear();
foreach (Item item in temp) mVideoOut.Add(item);



TYhi way I have "refreshed" the collection. I know it is not a smart way to implement it this way but I could not figure out the INotify issue. That is, if Item is derived from INotify how the collection is notified.

Can someone help me on this issue.

Regards,

Igal
Posted

I think your requirement is to refresh the UI when you directly assign your collection like mVideoOut = Items. If so, either create a normal property for mVideoOut and in the setter, raise property changed for the collection using INotifyPropertyChanged or alternatively you can use Dependency property.

Clarify if you have some other problem.
 
Share this answer
 
v2
An ObservableCollection has CollectionChanged Event. You can track this event and update your control whenever the ObservableCollection changed (Any item changed).

http://stackoverflow.com/questions/1427471/c-observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyp[^]

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8e9ef79d-e6cd-4cca-9934-24410dea522e[^]




HTH
 
Share this answer
 
Comments
Igal_Kroyter 28-Jul-10 15:42pm    
Reason for my vote of 4
I have found these answers and I figured where I was mistaken
I have figured out that my concept was incorrect.
Actually, I did not think through the whole process of updating.

Well the UI is bounded to an object inside an ObservableCollection via the Path. The UI is updated via two routes (I was thinking only one) via the ObservableCollection as a result of changes on this object (add,remove, etc.) and via changes in the item (if the item implements INotifyPropertyChanged).

I was thinking that "magically" the ObservableCollection is notified that an internal item was changed and then it notifies the UI which then goes via the Path and reads the new value from the item.

But Actually the ObservableCollection is out of the picture and someone has to invoke the set command of the item (property) which then invokes the OnPropertyChanged (as implemented in ViewModelBase.cs) and then the UI invokes the get.

C#
public int Item
{
    get { return mItem; }
    set
    {
        mItem= value;
        OnPropertyChanged("Item");// taken from ViewModelBase.cs
    }
}


Who is this someone: well it could be another property which is binded to another UI (eg button,slider) and then call the set from within its set. Or, as I did in case of changes in the Mosel itself (some communication going on there) is by implementing a DispatcherTimer which periodically reads a parameter in the model and updates the property which notifies the UI.

It is true that I could implement an event notification from the Model to the ModelView but it just does not sound to me MVVM (which I am trying to follow).

Igal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900