1、重写类ObservableCollection
public class DispatchedObservableCollection : ObservableCollection
{
DispatchEvent collectionChanged = new DispatchEvent();
DispatchEvent propertyChanged = new DispatchEvent();
HashSet changedPropertyList = new HashSet();
protected bool delayNotification;
protected bool DelayNotification
{
get { return delayNotification; }
set
{
if ( value == delayNotification )
return;
delayNotification = value;
if ( !delayNotification )
{
OnCollectionChanged( new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Reset ) );
BurstPropertyChangeNotification();
}
}
}
public override event NotifyCollectionChangedEventHandler CollectionChanged
{
add { collectionChanged.Add( value ); }
remove { collectionChanged.Remove( value ); }
}
protected override event PropertyChangedEventHandler PropertyChanged
{
add { propertyChanged.Add( value ); }
remove { propertyChanged.Remove( value ); }
}
protected override void OnCollectionChanged( NotifyCollectionChangedEventArgs e )
{
Debug.WriteLine( "DispatchedObservableCollection.OnCollectionChanged" );
if ( DelayNotification )
return;
collectionChanged.Fire( this, e );
}
protected override void OnPropertyChanged( PropertyChangedEventArgs e )
{
Debug.WriteLine( "DispatchedObservableCollection.OnPropertyChanged" );
changedPropertyList.Add( e.PropertyName );
if ( DelayNotification )
return;
BurstPropertyChangeNotification();
}
private void BurstPropertyChangeNotification()
{
foreach ( var propertyName in changedPropertyList )
propertyChanged.Fire( this, new PropertyChangedEventArgs( propertyName ) );
changedPropertyList.Clear();
}
public void AddRange( IEnumerable collection )
{
InsertRange( this.Count, collection );
}
public void InsertRange( int index, IEnumerable collection )
{
if ( collection == null )
throw new ArgumentNullException( "collection" );
if ( index > this.Count )
throw new ArgumentOutOfRangeException( "index" );
DelayNotification = true;
int i = 0;
foreach ( var item in collection )
{
this.InsertItem( index + i, item );
i++;
}
DelayNotification = false;
}
public void RemoveRange( int index, int count )
{
if ( ( index < 0 ) || ( count < 0 ) )
throw new ArgumentOutOfRangeException( index < 0 ? "index" : "count" );
if ( this.Count - index < count )
throw new ArgumentOutOfRangeException( "count", "Not enough element in the collection" );
DelayNotification = true;
var removedItems = new List();
for ( int i = 0; i < count; i++ )
{
removedItems.Add( this.Items[index] );
this.RemoveAt( index );
}
DelayNotification = false;
}
public void Remove( IEnumerable collection )
{
if ( collection == null )
throw new ArgumentNullException( "collection" );
DelayNotification = true;
foreach ( var item in collection )
Remove( item );
DelayNotification = false;
}
}
使用方式:
先定义一个集合
private DispatchedObservableCollection privateCollection = new DispatchedObservableCollection();
调用方法
private void AddIntItems()
{
var worker = new BackgroundWorker();
worker.DoWork += ( s, e ) =>
{
privateCollection.Clear();
for ( int i = 0; i < 10; i++ )
{
var list = new List();
var rnd = new Random( DateTime.Now.Millisecond );
for ( int j = 0; j < 5; j++ )
{
list.Add( rnd.Next( 2000 ) );
}
privateCollection.AddRange( list );
Thread.Sleep( 500 );
}
};
worker.RunWorkerAsync();
}
具体详情 ,查看该方法: https://archive.codeplex.com/?p=wpfextensions