melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices
https://melanchall.github.io/drywetmidi
MIT License
540 stars 75 forks source link

Events for changing a chunk-event-list #41

Closed FischertBilligheim closed 4 years ago

FischertBilligheim commented 4 years ago

Would it be possible to implement an event-handling for changes in a event-list of a chunk?

I think for using the lists in a datagridview with data-binding this would be necessary.

Regards Thomas

melanchall commented 4 years ago

Thomas,

Do you want EventsCollection to be something like ObservableCollection that can notify subscribers about collection changing?

FischertBilligheim commented 4 years ago

Yes, I think so. I‘m currently working on a midi-editor, based on your drywetmidi-library. I didn’t programmed since a lot of years, so a lot of things are unknown to me. I‘m currently using 2 datagridviews, in which I can show - in a human readable format - the events of a chunk. I implemented things like copy/paste/delete and Drag /drop. Also from one grid to the other. If an event of a chunk is deleted, and the other grid shows the same chunk, I need the possibility to be notified. (The Datagrids are encapsulated in a user-Control, I‘m currently filling the grids - and I‘m not using binding)

I yesterday did a test with binding: I added „..chunk.events.ToList()“ to the binding of a datagridview - the grid then shows 2 columns of the events: the type and the delta-time. But when I delete an entry of the events (from outside), the datagridview does not update....

I hope I could explane my needs 😉

melanchall commented 4 years ago

Thanks for explanation!

I think current implementation of EventsCollection shouldn't be changed since notifying about changes can cause performance penalties. Instead you should use another collection classes dumping all events into them before binding to UI controls.

For example, you read a MIDI file and want to use events collection of first track chunk as binding source for your DataGridView. In this case create dedicated collection class instance and copy all original events into it:

var originalEvents = trackChunk.Events;
var bindableCollection = new BindingList<MidiEvent>(originalEvents.ToList());

Then use bindableCollection as binding source for UI control. When you are done with MIDI data in your MIDI editor, just replace all original events with those contained in the bindableCollection:

originalEvents.Clear();
originalEvents.AddRange(bindableCollection);

Please let me know if it's worked.

FischertBilligheim commented 4 years ago

Sure, this will work, but....

I‘m currently working directly at the chunks and events from DryWetMIDI. I‘m also deleting, merging and creating new chunks. To use own collections means to use the chunks-classes only for serializing the midi file. That‘s a pity. And by the way: I‘m also using playback etc.... But as I wrote: Sure, I can do that..., Thanks a lot! Thomas

melanchall commented 4 years ago

Although you've closed the issue, let me explain why you should create appropriate collections on your side.

Different frameworks requires different interfaces to support changes notification. WinForms works with IBindingList, WPF with INotifyCollectionChanged, some other frameworks requires another interfaces (probably those are not a part of standard .NET class library). So it's a bad idea to implement them all.

Also data binding is a concept related to MVVM, where there is a separation between data and UI. DryWetMIDI provides a layer that relates to data (model in terms of MVVM). Interaction with UI is absolutely up to user of the library.