Michel Keijzers
Michel Keijzers

Reputation: 15367

MVVM INotifyPropertyChanged in dynamically changed arrays

In my hobby project I have the following situation:

I'm using C#, WPF, MVVM (well MVVM: try to)

I load a binary file with information and store it in a list of (unsigned) bytes. I have a class hierarchy referencing to specific locations in the list. My application changes sometimes the class hierarchy (and the referencing bytes), but also sometimes bytes directly (like copying a range).

I have windows in where I have bound a list view to the mapped structure.

When changing properties in the classes I use the INotifyPropertyChanged as it should be used in MVVM. However, I have problems when changing the bytes directly. Currently I'm using manual Update methods but it's getting very messy.

Is there some kind of solution for this?

Also the list of bytes can be like 45 MB of data. If I set a notification on the list would that mean that for every byte change I get a notify on all observers? Meaning when I would copy e.g. 10KB of bytes one by one, and I would have like 100 observer functions (handling property changes) in 1 million updates? That would be a performance killer.

Or can I also say something like: now update class X (i.e. all property changes inside a class). I cannot use the set property function because the values already have been changed while copying bytes.

Upvotes: 0

Views: 1010

Answers (2)

Moha Dehghan
Moha Dehghan

Reputation: 18462

If you are storing your bytes in a list (List<byte>) then you could simpley change the type of the list to ObservableCollection<byte> and your code will run with nearly no change. Then you can get rid of manual updates.

But if you hold the data in a byte array (byte[]), again the best option is changing it to ObservableCollection<byte>, but you may have to change some of your codes using the colletion.

If you have special cases to handle, another option may be to create your own data structure, implementing INotifyPropertyChanged and INotifyCollectionChanged. But this is lots of code to write.

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190976

You could do your binding to an ObservableCollection<T>.

Upvotes: 2

Related Questions