namespace Core
{
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Threading;
///
/// This class is an Observable Collection which invokes automatically.
/// This means that any change will be done in the right thread.
///
/// The type of the elements
public class DispatcherObservableCollection : ObservableCollection
{
///
/// The default constructor of the ObservableCollection
///
public DispatcherObservableCollection()
{
//Assign the current Dispatcher (owner of the collection)
this.currentDispatcher = Dispatcher.CurrentDispatcher;
}
private readonly Dispatcher currentDispatcher;
///
/// Executes this action in the right thread
///
///The action which should be executed
private void DoDispatchedAction(Action action)
{
if (this.currentDispatcher.CheckAccess())
{
action.Invoke();
}
else
{
this.currentDispatcher.Invoke(DispatcherPriority.DataBind, action);
}
}
///
/// Clears all items
///
protected override void ClearItems()
{
this.DoDispatchedAction(this.BaseClearItems);
}
private void BaseClearItems()
{
base.ClearItems();
}
///
/// Inserts a item at the specified index
///
///The index where the item should be inserted
///The item which should be inserted
protected override void InsertItem(int index, T item)
{
this.DoDispatchedAction(() => this.BaseInsertItem(index, item));
}
private void BaseInsertItem(int index, T item)
{
base.InsertItem(index, item);
}
///
/// Moves an item from one index to another
///
///The index of the item which should be moved
///The index where the item should be moved
protected override void MoveItem(int oldIndex, int newIndex)
{
this.DoDispatchedAction(() => this.BaseMoveItem(oldIndex, newIndex));
}
private void BaseMoveItem(int oldIndex, int newIndex)
{
base.MoveItem(oldIndex, newIndex);
}
///
/// Removes the item at the specified index
///
///The index of the item which should be removed
protected override void RemoveItem(int index)
{
this.DoDispatchedAction(() => this.BaseRemoveItem(index));
}
private void BaseRemoveItem(int index)
{
base.RemoveItem(index);
}
///
/// Sets the item at the specified index
///
///The index which should be set
///The new item
protected override void SetItem(int index, T item)
{
this.DoDispatchedAction(() => this.BaseSetItem(index, item));
}
private void BaseSetItem(int index, T item)
{
base.SetItem(index, item);
}
///
/// Fires the CollectionChanged Event
///
///The additional arguments of the event
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
{
this.DoDispatchedAction(() => this.BaseOnCollectionChanged(args));
}
private void BaseOnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
}
///
/// Fires the PropertyChanged Event
///
///The additional arguments of the event
protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{
this.DoDispatchedAction(() => this.BaseOnPropertyChanged(args));
}
private void BaseOnPropertyChanged(PropertyChangedEventArgs args)
{
base.OnPropertyChanged(args);
}
}
}