- XamlPatterns_v1.1.0.0.zip
- Controls
- Core
- docu
- Program
- Pub
- Desktop.Debug
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- Desktop.Release
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- Silverlight.Debug
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- Silverlight.Release
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- WindowsPhone.Debug
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- WindowsPhone.Release
- Itenso.Community.XamlPatterns.Controls.dll
- Itenso.Community.XamlPatterns.dll
- XamlPatterns.Desktop.sln
- XamlPatterns.Desktop.suo
- XamlPatterns.Silverlight.sln
- XamlPatterns.Silverlight.suo
- XamlPatterns.WindowsPhone.sln
- XamlPatterns.WindowsPhone.suo
|
// -- 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.
👨 Senior .NET Software Engineer
🚀 My Open Source Projects
-
Time Period Library 👉
GitHub
-
Payroll Engine 👉
GitHub
Feedback and contributions are welcome.