In my WPF application, I have an ItemsControl element whose element values ββdepend on the previous element displayed .
The ViewModel is an audio file divided into variable length parts, and I need to display it this way, with the DateTime value displayed on the right and what I need to calculate (I know only the length of each part, I need to calculate the actual start and end time, as well position in ItemsControl).
-- ---- ------------ -- --------------------
My first approach was to use an ObservableCollection<MyviewModel> , but some horrors soon occurred:
5-way multibinding, in which IMultiValueConverter I would calculate the return value and set the DataContext property for this value, because I knew only the previous element at runtime.
The previous item was sent using the binding on Relativesource.PreviousData .
Now my problem is that after setting the value from the converter (which is obviously bad) and actually making it work, the regular collection has no concept of order in its elements, therefore, when the road is further down, when I want to add an audio part to in the middle of the rest, the display is messed up.
In addition, when I use more business logic, I may need access to the beginning and end of the audio components that are calculated in this converter, and what if it is not already displayed ...?
So this approach was wrong on several levels.
What I started to search on Google and learned about LinkedList . Now I'm trying to create a class that is basically an Observable LinkedList (I don't need it to be shared):
public class ObservableSegmentLinkedList : LinkedList<MyViewModel>, INotifyCollectionChanged { //Overrides ???
And the crux of the problem is that I cannot override methods that modify the collection (Addfirst, AddLast, etc.), so I cannot correctly call OnNotifyCollectionChanged ...
So, I think I can do overloads for each of these methods, but that sounds pretty unpleasant ...
In short: I need some kind of collection in which each element knows the details of the previous one in order to calculate one of its properties.
Any clues? is this even a good solution?
Thanks!
Application, ViewModel looks like this:
public class MyViewModel : INotifyPropertyChanged { private DateTime m_SegmentLength; public DateTime SegmentLength { get { return m_SegmentLength; } set { m_SegmentLength = value; NotifyPropertyChanged("SegmentLength"); } } private DateTime m_SegmentAdvert; public DateTime SegmentAdvert { get { return m_SegmentAdvert; } set { m_SegmentAdvert = value; NotifyPropertyChanged("SegmentAdvert"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String prop) { this.PropertyChanged(this, new PropertyChangedEventArgs(prop)); } #endregion }
EDIT: I think I will try to combine the answers of Thomas and Will: I will use composition (i.e. I save a LinkedList instance in my user object instead of inheriting from it) and redefine the methods to be used (AddAfter, AddFirst, etc.) e.), in which I simply call OnNotifyPropertychanged after calling the LinkedList method. This is a bit of work, but I think there will be no elegant solution to my problem ...