Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps / Windows Phone 7

Presentation Patterns for XAML based Applications

Rate me:
Please Sign up or sign in to vote.
4.99/5 (44 votes)
17 Sep 2013CPOL23 min read 94.2K   1.5K   176  
Design patterns on the presentation layer for WPF, Silverlight and Windows Phone applications.
// -- FILE ------------------------------------------------------------------
// name       : ItemCollection.cs
// project    : Itenso Community
// created    : Jani Giannoudis - 2012.05.05
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2004-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Itenso.Community.XamlPatterns.Actions.Data;

namespace Itenso.Community.XamlPatterns.Collection
{

	// ------------------------------------------------------------------------
	public class ItemCollection<TItem> : ObservableCollection<TItem>, IItemCollection<TItem>, IClearable
	{

		// ----------------------------------------------------------------------
		public bool IsUpdating
		{
			get { return updateCount > 0; }
		} // IsSetuping

		// ----------------------------------------------------------------------
		// see http://mokosh.co.uk/post/2009/08/04/how-to-sort-observablecollection/
		private List<TItem> ItemsAsList
		{
			get { return Items as List<TItem>; }
		} // ItemsAsList

		// ----------------------------------------------------------------------
		public virtual void BeginUpdate()
		{
			updateCount++;
		} // BeginUpdate

		// ----------------------------------------------------------------------
		public virtual void EndUpdate()
		{
			if ( updateCount == 0 )
			{
				throw new InvalidOperationException();
			}
			updateCount--;
			UpdateCollectionChanged();
		} // EndUpdate

		// ----------------------------------------------------------------------
		public virtual void AddAll( IEnumerable<TItem> items )
		{
			if ( items == null )
			{
				throw new ArgumentNullException( "items" );
			}

			foreach ( TItem item in items )
			{
				Add( item );
			}
		} // AddAll

		// ----------------------------------------------------------------------
		public virtual void Replace( TItem oldItem, TItem newItem )
		{
			int index = IndexOf( oldItem );
			if ( index < 0 )
			{
				throw new ArgumentException( "oldItem" );
			}

			Items[ index ] = newItem;
			DisposeItem( oldItem );
			OnCollectionChanged( new NotifyCollectionChangedEventArgs(
				NotifyCollectionChangedAction.Replace, newItem, oldItem, index ) );
		} // Replace

		// ----------------------------------------------------------------------
		public virtual void Sort()
		{
			ItemsAsList.Sort();
			UpdateCollectionChanged();
		} // Sort

		// ----------------------------------------------------------------------
		public virtual void Sort( Comparison<TItem> comparison )
		{
			if ( comparison == null )
			{
				throw new ArgumentNullException( "comparison" );
			}

			ItemsAsList.Sort( comparison );
			UpdateCollectionChanged();
		} // Sort

		// ----------------------------------------------------------------------
		public virtual void Sort( IComparer<TItem> comparer )
		{
			if ( comparer == null )
			{
				throw new ArgumentNullException( "comparer" );
			}

			ItemsAsList.Sort( comparer );
			UpdateCollectionChanged();
		} // Sort

		// ----------------------------------------------------------------------
		public virtual void Sort( int index, int count, IComparer<TItem> comparer )
		{
			if ( comparer == null )
			{
				throw new ArgumentNullException( "comparer" );
			}

			ItemsAsList.Sort( index, count, comparer );
			UpdateCollectionChanged();
		} // Sort

		// ----------------------------------------------------------------------
		public void Dispose()
		{
			if ( updateCount > 0 )
			{
				throw new InvalidOperationException( "unbalanced collection dispose" );
			}
			Dispose( true );
			GC.SuppressFinalize( this );
		} // Dispose

		// ----------------------------------------------------------------------
		protected virtual void Dispose( bool disposing )
		{
			if ( disposing )
			{
				DisposeItems( this );
			}
		} // Dispose

		// ----------------------------------------------------------------------
		protected override void ClearItems()
		{
			DisposeItems( this );
			base.ClearItems();
		} // ClearItems

		// ----------------------------------------------------------------------
		protected override void RemoveItem( int index )
		{
			DisposeItem( this[ index ] );
			base.RemoveItem( index );
		} // RemoveItem

		// ----------------------------------------------------------------------
		protected override void OnCollectionChanged( NotifyCollectionChangedEventArgs e )
		{
			if ( IsUpdating )
			{
				// break the workflow to reduce the UI communication
				return;
			}

			base.OnCollectionChanged( e );
		} // OnCollectionChanged

		// ----------------------------------------------------------------------
		protected void UpdateCollectionChanged()
		{
			if ( IsUpdating )
			{
				return;
			}
			OnCollectionChanged( new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Reset ) );
		} // UpdateCollectionChanged

		// ----------------------------------------------------------------------
		private static void DisposeItems( IEnumerable items )
		{
			foreach ( TItem item in items )
			{
				DisposeItem( item );
			}
		} // DisposeItems

		// ----------------------------------------------------------------------
		private static void DisposeItem( TItem item )
		{
			IDisposable disposable = item as IDisposable;
			if ( disposable != null )
			{
				disposable.Dispose();
			}
		} // DisposeItem

		// ----------------------------------------------------------------------
		// members
		private int updateCount;

	} // class ItemCollection

} // namespace Itenso.Community.XamlPatterns.Collection
// -- EOF -------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions