WPF 通过后台线程添加(DispatchedObservableCollection)集合元素类

1、重写类ObservableCollection

public class DispatchedObservableCollection<T> : ObservableCollection<T>
	{
		DispatchEvent collectionChanged = new DispatchEvent();
		DispatchEvent propertyChanged = new DispatchEvent();
		HashSet<string> changedPropertyList = new HashSet<string>();

		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<T> collection )
		{
			InsertRange( this.Count, collection );
		}

		public void InsertRange( int index, IEnumerable<T> 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<T>();
			for ( int i = 0; i < count; i++ )
			{
				removedItems.Add( this.Items[index] );
				this.RemoveAt( index );
			}
			DelayNotification = false;
		}

		public void Remove( IEnumerable<T> collection )
		{
			if ( collection == null )
				throw new ArgumentNullException( "collection" );
			DelayNotification = true;
			foreach ( var item in collection )
				Remove( item );
			DelayNotification = false;
		}
	}

使用方式:

先定义一个集合

private DispatchedObservableCollection<int> privateCollection = new DispatchedObservableCollection<int>();

调用方法 

private void AddIntItems()
		{
			var worker = new BackgroundWorker();
			worker.DoWork += ( s, e ) =>
			{
				privateCollection.Clear();
				for ( int i = 0; i < 10; i++ )
				{
					var list = new List<int>();
					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

猜你喜欢

转载自blog.csdn.net/qq_28368039/article/details/105504754
WPF