using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
namespace LiveCharts.Uwp.Components.MultiBinding
{
///
/// Represents a collection of instances of a specified type.
///
/// The type of items in the collection.
public class DependencyObjectCollection : DependencyObjectCollection, INotifyCollectionChanged
where T : DependencyObject
{
///
/// Occurs when items in the collection are added, removed, or replaced.
///
public event NotifyCollectionChangedEventHandler CollectionChanged;
private readonly List _oldItems = new List();
///
/// Initializes a new instance of the class.
///
public DependencyObjectCollection()
{
VectorChanged += DependencyObjectCollectionVectorChanged;
}
private void DependencyObjectCollectionVectorChanged(IObservableVector sender, IVectorChangedEventArgs e)
{
var index = (int)e.Index;
switch (e.CollectionChange)
{
case CollectionChange.Reset:
foreach (var item in this)
{
VerifyType(item);
}
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
_oldItems.Clear();
break;
case CollectionChange.ItemInserted:
VerifyType(this[index]);
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this[index], index));
_oldItems.Insert(index, (T)this[index]);
break;
case CollectionChange.ItemRemoved:
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, _oldItems[index], index));
_oldItems.RemoveAt(index);
break;
case CollectionChange.ItemChanged:
VerifyType(this[index]);
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, this[index], _oldItems[index]));
_oldItems[index] = (T)this[index];
break;
default:
throw new ArgumentOutOfRangeException();
}
}
///
/// Raises the event with the provided event data.
///
/// The event data.
protected void RaiseCollectionChanged(NotifyCollectionChangedEventArgs eventArgs)
{
var eventHandler = CollectionChanged;
if (eventHandler != null)
{
eventHandler(this, eventArgs);
}
}
private void VerifyType(DependencyObject item)
{
if (!(item is T))
{
throw new InvalidOperationException("Invalid item type added to collection");
}
}
}
}