Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / MS / Internal / Annotations / AnnotationObservableCollection.cs / 1 / AnnotationObservableCollection.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) 2003 by Microsoft Corporation. All rights reserved.
//
//
//
// Description: Subclass of ObservableCollection which also registers for
// INotifyPropertyChanged on each of its items (if T implements
// INotifyPropertyChanged) and passes on the events via the
// ItemChanged event.
//
// History:
// 03/10/2005 : rruiz - created
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
namespace MS.Internal.Annotations
{
// An internal extension of INotifyPropertyChanged introduced in order to keep
// our use of it internal and not publicly expose this interface on our OM.
internal interface INotifyPropertyChanged2 : INotifyPropertyChanged
{
}
///
///
internal class AnnotationObservableCollection : ObservableCollection where T : INotifyPropertyChanged2, IOwnedObject
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructors
///
/// Initializes a new instance of AnnotationObservableCollection that is empty and has default initial capacity.
///
public AnnotationObservableCollection() : base()
{
_listener = new PropertyChangedEventHandler(OnItemPropertyChanged);
}
///
/// Initializes a new instance of the AnnotationObservableCollection class
/// that contains elements copied from the specified list
///
/// The list whose elements are copied to the new list.
///
/// The elements are copied onto the AnnotationObservableCollection in the
/// same order they are read by the enumerator of the list.
///
/// list is a null reference
public AnnotationObservableCollection(List list) : base(list)
{
_listener = new PropertyChangedEventHandler(OnItemPropertyChanged);
}
#endregion Constructors
//------------------------------------------------------
//
// Public Events
//
//-----------------------------------------------------
//------------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
#region Protected Methods
///
/// called by base class Collection<T> when the list is being cleared;
/// raises a CollectionChanged event to any listeners
///
protected override void ClearItems()
{
foreach (INotifyPropertyChanged2 item in this)
{
SetOwned(item, false);
}
ProtectedClearItems();
}
///
/// called by base class Collection<T> when an item is removed from list;
/// raises a CollectionChanged event to any listeners
///
protected override void RemoveItem(int index)
{
T removedItem = this[index];
SetOwned(removedItem, false);
base.RemoveItem(index);
}
///
/// called by base class Collection<T> when an item is added to list;
/// raises a CollectionChanged event to any listeners
///
protected override void InsertItem(int index, T item)
{
if (ItemOwned(item))
throw new ArgumentException(SR.Get(SRID.AlreadyHasParent));
base.InsertItem(index, item);
SetOwned(item, true);
}
///
/// called by base class Collection<T> when an item is added to list;
/// raises a CollectionChanged event to any listeners
///
protected override void SetItem(int index, T item)
{
if (ItemOwned(item))
throw new ArgumentException(SR.Get(SRID.AlreadyHasParent));
T originalItem = this[index];
SetOwned(originalItem, false);
ProtectedSetItem(index, item);
SetOwned(item, true);
}
///
/// Virtual methods allowing subclasses to change the eventing
/// behavior for the ClearItems method. The default behavior
/// is to call ObservableCollection's method.
///
protected virtual void ProtectedClearItems()
{
// Use the standard built-in event
base.ClearItems();
}
///
/// Virtual methods allowing subclasses to change the eventing
/// behavior for the SetItem method. The default behavior
/// is to call Collection's defaut method and fire a single
/// event.
///
/// index of the item being set
/// item to set at that index
protected virtual void ProtectedSetItem(int index, T item)
{
// We only want to fire one event here - this collection contains items that
// are within a Resource so an assignment means one resource change, period.
Items[index] = item; // directly set Collection inner Items collection
OnPropertyChanged(new PropertyChangedEventArgs(CountString));
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
///
/// Allows subclasses to call superclass's SetItem method without
/// any additional functionality added by this class.
///
/// index of item being set
/// item to set in that index
protected void ObservableCollectionSetItem(int index, T item)
{
base.SetItem(index, item);
}
// raise CollectionChanged event to any listeners
///
/// When an item we contain fires a PropertyChanged event we fire
/// a collection changed event letting listeners know the collection
/// has changed in some way. We don't care about the particulars of
/// the event - just want to pass up the chain of objects that something
/// has changed.
///
/// the object whose property changed
/// the event args describing the property that changed
protected virtual void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
#endregion Protected Methods
//-----------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
// returns whether this item already belongs to a parent object
private bool ItemOwned(Object item)
{
if (item != null)
{
IOwnedObject obj = item as IOwnedObject;
return obj.Owned;
}
return false;
}
// sets whether this object belongs to a parent object
private void SetOwned(Object item, bool owned)
{
if (item != null)
{
IOwnedObject obj = item as IOwnedObject;
obj.Owned = owned;
if (owned)
{
((INotifyPropertyChanged2)item).PropertyChanged += _listener;
}
else
{
((INotifyPropertyChanged2)item).PropertyChanged -= _listener;
}
}
}
#endregion Private Methods
//-----------------------------------------------------
//
// Private Fields
//
//-----------------------------------------------------
#region Private & Internal Fields
private readonly PropertyChangedEventHandler _listener = null;
internal readonly string CountString = "Count";
internal readonly string IndexerName = "Item[]";
#endregion Private & Internal Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) 2003 by Microsoft Corporation. All rights reserved.
//
//
//
// Description: Subclass of ObservableCollection which also registers for
// INotifyPropertyChanged on each of its items (if T implements
// INotifyPropertyChanged) and passes on the events via the
// ItemChanged event.
//
// History:
// 03/10/2005 : rruiz - created
//
//---------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
namespace MS.Internal.Annotations
{
// An internal extension of INotifyPropertyChanged introduced in order to keep
// our use of it internal and not publicly expose this interface on our OM.
internal interface INotifyPropertyChanged2 : INotifyPropertyChanged
{
}
///
///
internal class AnnotationObservableCollection : ObservableCollection where T : INotifyPropertyChanged2, IOwnedObject
{
//-----------------------------------------------------
//
// Constructors
//
//-----------------------------------------------------
#region Constructors
///
/// Initializes a new instance of AnnotationObservableCollection that is empty and has default initial capacity.
///
public AnnotationObservableCollection() : base()
{
_listener = new PropertyChangedEventHandler(OnItemPropertyChanged);
}
///
/// Initializes a new instance of the AnnotationObservableCollection class
/// that contains elements copied from the specified list
///
/// The list whose elements are copied to the new list.
///
/// The elements are copied onto the AnnotationObservableCollection in the
/// same order they are read by the enumerator of the list.
///
/// list is a null reference
public AnnotationObservableCollection(List list) : base(list)
{
_listener = new PropertyChangedEventHandler(OnItemPropertyChanged);
}
#endregion Constructors
//------------------------------------------------------
//
// Public Events
//
//-----------------------------------------------------
//------------------------------------------------------
//
// Protected Methods
//
//------------------------------------------------------
#region Protected Methods
///
/// called by base class Collection<T> when the list is being cleared;
/// raises a CollectionChanged event to any listeners
///
protected override void ClearItems()
{
foreach (INotifyPropertyChanged2 item in this)
{
SetOwned(item, false);
}
ProtectedClearItems();
}
///
/// called by base class Collection<T> when an item is removed from list;
/// raises a CollectionChanged event to any listeners
///
protected override void RemoveItem(int index)
{
T removedItem = this[index];
SetOwned(removedItem, false);
base.RemoveItem(index);
}
///
/// called by base class Collection<T> when an item is added to list;
/// raises a CollectionChanged event to any listeners
///
protected override void InsertItem(int index, T item)
{
if (ItemOwned(item))
throw new ArgumentException(SR.Get(SRID.AlreadyHasParent));
base.InsertItem(index, item);
SetOwned(item, true);
}
///
/// called by base class Collection<T> when an item is added to list;
/// raises a CollectionChanged event to any listeners
///
protected override void SetItem(int index, T item)
{
if (ItemOwned(item))
throw new ArgumentException(SR.Get(SRID.AlreadyHasParent));
T originalItem = this[index];
SetOwned(originalItem, false);
ProtectedSetItem(index, item);
SetOwned(item, true);
}
///
/// Virtual methods allowing subclasses to change the eventing
/// behavior for the ClearItems method. The default behavior
/// is to call ObservableCollection's method.
///
protected virtual void ProtectedClearItems()
{
// Use the standard built-in event
base.ClearItems();
}
///
/// Virtual methods allowing subclasses to change the eventing
/// behavior for the SetItem method. The default behavior
/// is to call Collection's defaut method and fire a single
/// event.
///
/// index of the item being set
/// item to set at that index
protected virtual void ProtectedSetItem(int index, T item)
{
// We only want to fire one event here - this collection contains items that
// are within a Resource so an assignment means one resource change, period.
Items[index] = item; // directly set Collection inner Items collection
OnPropertyChanged(new PropertyChangedEventArgs(CountString));
OnPropertyChanged(new PropertyChangedEventArgs(IndexerName));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
///
/// Allows subclasses to call superclass's SetItem method without
/// any additional functionality added by this class.
///
/// index of item being set
/// item to set in that index
protected void ObservableCollectionSetItem(int index, T item)
{
base.SetItem(index, item);
}
// raise CollectionChanged event to any listeners
///
/// When an item we contain fires a PropertyChanged event we fire
/// a collection changed event letting listeners know the collection
/// has changed in some way. We don't care about the particulars of
/// the event - just want to pass up the chain of objects that something
/// has changed.
///
/// the object whose property changed
/// the event args describing the property that changed
protected virtual void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
#endregion Protected Methods
//-----------------------------------------------------
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
// returns whether this item already belongs to a parent object
private bool ItemOwned(Object item)
{
if (item != null)
{
IOwnedObject obj = item as IOwnedObject;
return obj.Owned;
}
return false;
}
// sets whether this object belongs to a parent object
private void SetOwned(Object item, bool owned)
{
if (item != null)
{
IOwnedObject obj = item as IOwnedObject;
obj.Owned = owned;
if (owned)
{
((INotifyPropertyChanged2)item).PropertyChanged += _listener;
}
else
{
((INotifyPropertyChanged2)item).PropertyChanged -= _listener;
}
}
}
#endregion Private Methods
//-----------------------------------------------------
//
// Private Fields
//
//-----------------------------------------------------
#region Private & Internal Fields
private readonly PropertyChangedEventHandler _listener = null;
internal readonly string CountString = "Count";
internal readonly string IndexerName = "Item[]";
#endregion Private & Internal Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SecurityDescriptor.cs
- PermissionListSet.cs
- StrongNameIdentityPermission.cs
- DataControlPagerLinkButton.cs
- File.cs
- ColorAnimationUsingKeyFrames.cs
- Sql8ConformanceChecker.cs
- SamlAuthenticationClaimResource.cs
- ListSourceHelper.cs
- Panel.cs
- CookieHandler.cs
- MulticastOption.cs
- CodeDomExtensionMethods.cs
- MobileUITypeEditor.cs
- Pkcs7Signer.cs
- SecUtil.cs
- QilExpression.cs
- MachineKeyConverter.cs
- VectorValueSerializer.cs
- DbProviderServices.cs
- ScrollContentPresenter.cs
- _NegoState.cs
- DbDataReader.cs
- DataGridViewColumnCollectionEditor.cs
- SmiSettersStream.cs
- SerializationEventsCache.cs
- TraceSwitch.cs
- GlyphsSerializer.cs
- SiteMap.cs
- StringValidator.cs
- TableSectionStyle.cs
- ThumbButtonInfo.cs
- EpmCustomContentDeSerializer.cs
- ControlBindingsConverter.cs
- DoWorkEventArgs.cs
- DPTypeDescriptorContext.cs
- Image.cs
- Avt.cs
- SeekableReadStream.cs
- Fonts.cs
- ResourcePool.cs
- GenericUriParser.cs
- RegexWorker.cs
- AvTraceDetails.cs
- XsltSettings.cs
- EditorPart.cs
- Token.cs
- SelectionProcessor.cs
- PrimitiveSchema.cs
- DetailsViewPageEventArgs.cs
- _ListenerRequestStream.cs
- ColorAnimationBase.cs
- AnnotationAdorner.cs
- XmlNullResolver.cs
- TextEmbeddedObject.cs
- XmlAttribute.cs
- RadioButtonFlatAdapter.cs
- StorageConditionPropertyMapping.cs
- MemberNameValidator.cs
- objectquery_tresulttype.cs
- CodeGeneratorOptions.cs
- ButtonField.cs
- WorkflowDesignerMessageFilter.cs
- ProviderUtil.cs
- StrokeFIndices.cs
- ProcessProtocolHandler.cs
- RegisteredArrayDeclaration.cs
- DrawListViewSubItemEventArgs.cs
- propertyentry.cs
- PreservationFileWriter.cs
- oledbmetadatacollectionnames.cs
- WebPartConnection.cs
- FrameworkContentElementAutomationPeer.cs
- _UriSyntax.cs
- CngAlgorithmGroup.cs
- DesignerOptionService.cs
- TextUtf8RawTextWriter.cs
- LongTypeConverter.cs
- HitTestParameters3D.cs
- __TransparentProxy.cs
- ThicknessConverter.cs
- InheritanceService.cs
- Constraint.cs
- GeneralTransform3DTo2D.cs
- RuntimeComponentFilter.cs
- ExpandSegmentCollection.cs
- SizeAnimationClockResource.cs
- BindingsCollection.cs
- ConfigXmlAttribute.cs
- StrokeFIndices.cs
- MenuAdapter.cs
- RectIndependentAnimationStorage.cs
- ImplicitInputBrush.cs
- LineServicesRun.cs
- ValidationEventArgs.cs
- ExceptionRoutedEventArgs.cs
- TextBlock.cs
- WsdlBuildProvider.cs
- ProcessHostServerConfig.cs
- AutomationElementIdentifiers.cs