I am trying to copy an ObservableCollection into an ObservableCollection-derived type, and I want to preserve the CollectionChanged event.
So far I have:
public class PartitionCollection : ObservableCollection<AislePartition>
{
public PartitionCollection(ObservableCollection<AislePartition> other)
: base(other)
{
}
// ...
protected override void InsertItem(int index, AislePartition item)
{
// Important custom logic runs here
if (/* */)
base.InsertItem(index, item);
else
Merge(item);
}
protected void Merge(AislePartition item)
{
// ...
}
}
It copies the collection fine but I do need to get the CollectionChanged event too.
Any way of doing that ? Thanks
EDIT:
Before:
After:
The code that uses this particular constructor:
private static void OnSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
AisleSelection aisleSelect = args.NewValue as AisleSelection;
if (aisleSelect.Partitions == null)
aisleSelect.Partitions = new PartitionCollection();
else
aisleSelect.Partitions = new PartitionCollection(aisleSelect.Partitions);
...
}
Essentially what I'm trying to do is replace the ObservableCollection with my PartitionCollection which overrides a few key members. The ObservableCollection is passed down to me from the server in serialized form, so there is no way I can use it directly.