86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace JD.Guinness.WPF.Module
|
|
{
|
|
public sealed class DispatcherObservableCollection<T> : ObservableCollection<T>
|
|
{
|
|
private readonly Dispatcher _dispatcher;
|
|
|
|
public DispatcherObservableCollection(IEnumerable<T> items)
|
|
: base(items)
|
|
{
|
|
_dispatcher = Application.Current.Dispatcher;
|
|
}
|
|
|
|
public DispatcherObservableCollection()
|
|
: this(Enumerable.Empty<T>())
|
|
{ }
|
|
|
|
protected override void InsertItem(int index, T item)
|
|
{
|
|
if (!_dispatcher.CheckAccess())
|
|
{
|
|
_dispatcher.Invoke(() => base.InsertItem(index, item));
|
|
}
|
|
else
|
|
{
|
|
base.InsertItem(index, item);
|
|
}
|
|
}
|
|
|
|
protected override void RemoveItem(int index)
|
|
{
|
|
if (!_dispatcher.CheckAccess())
|
|
{
|
|
_dispatcher.Invoke(() => base.RemoveItem(index));
|
|
}
|
|
else
|
|
{
|
|
base.RemoveItem(index);
|
|
}
|
|
}
|
|
|
|
protected override void MoveItem(int oldIndex, int newIndex)
|
|
{
|
|
if (!_dispatcher.CheckAccess())
|
|
{
|
|
_dispatcher.Invoke(() => base.MoveItem(oldIndex, newIndex));
|
|
}
|
|
else
|
|
{
|
|
base.MoveItem(oldIndex, newIndex);
|
|
}
|
|
}
|
|
|
|
protected override void SetItem(int index, T item)
|
|
{
|
|
if (!_dispatcher.CheckAccess())
|
|
{
|
|
_dispatcher.Invoke(() => base.SetItem(index, item));
|
|
}
|
|
else
|
|
{
|
|
base.SetItem(index, item);
|
|
}
|
|
}
|
|
|
|
protected override void ClearItems()
|
|
{
|
|
if (!_dispatcher.CheckAccess())
|
|
{
|
|
_dispatcher.Invoke(ClearItems);
|
|
}
|
|
else
|
|
{
|
|
while (Count > 0)
|
|
{
|
|
base.RemoveItem(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|