Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / ObservableCollectionDefaultValueFactory.cs / 1305600 / ObservableCollectionDefaultValueFactory.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: DefaultvalueFactory for ObservableCollection
//
// History:
// 2009/24/07 : [....] - Created.
//
//---------------------------------------------------------------------------
using MS.Internal.WindowsBase;
using System;
using System.Diagnostics;
using System.Windows;
using System.Collections.ObjectModel; // ObservableCollection
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler
namespace MS.Internal
{
//
// ObservableCollectionDefaultValueFactory is a DefaultValueFactory implementation which will
// promote the default value to a local value if the collection is modified.
//
[FriendAccessAllowed] // built into Base, used by Framework
internal class ObservableCollectionDefaultValueFactory : DefaultValueFactory
{
internal ObservableCollectionDefaultValueFactory()
{
_default = new ObservableCollection();
}
///
/// This is used for Sealed objects. ObservableCollections are inherently mutable, so they shouldn't
/// be used with sealed objects. The PropertyDescriptor calls this, so we'll just return the same empty collection.
///
internal override object DefaultValue
{
get
{
return _default;
}
}
///
///
internal override object CreateDefaultValue(DependencyObject owner, DependencyProperty property)
{
Debug.Assert(owner != null && property != null,
"It is the caller responsibility to ensure that owner and property are non-null.");
var result = new ObservableCollection();
// Wire up a ObservableCollectionDefaultPromoter to observe the default value we
// just created and automatically promote it to local if it is modified.
// NOTE: We do not holding a reference to this because it should have the same lifetime as
// the collection. It will not be immediately GC'ed because it hooks the collections change event.
new ObservableCollectionDefaultPromoter(owner, property, result);
return result;
}
///
/// The ObservableCollectionDefaultPromoter observes the mutable defaults we hand out
/// for changed events. If the default is ever modified this class will
/// promote it to a local value by writing it to the local store and
/// clear the cached default value so we will generate a new default
/// the next time the property system is asked for one.
///
private class ObservableCollectionDefaultPromoter
{
internal ObservableCollectionDefaultPromoter(DependencyObject owner, DependencyProperty property, ObservableCollection collection)
{
Debug.Assert(owner != null && property != null,
"Caller is responsible for ensuring that owner and property are non-null.");
Debug.Assert(property.GetMetadata(owner.DependencyObjectType).UsingDefaultValueFactory,
"How did we end up observing a mutable if we were not registered for the factory pattern?");
// We hang on to the property and owner so we can write the default
// value back to the local store if it changes. See also
// OnDefaultValueChanged.
_owner = owner;
_property = property;
_collection = collection;
_collection.CollectionChanged += OnDefaultValueChanged;
}
internal void OnDefaultValueChanged(object sender, NotifyCollectionChangedEventArgs e)
{
PropertyMetadata metadata = _property.GetMetadata(_owner.DependencyObjectType);
// Remove this value from the DefaultValue cache so we stop
// handing it out as the default value now that it has changed.
metadata.ClearCachedDefaultValue(_owner, _property);
// If someone else hasn't already written a local value,
// promote the default value to local.
if (_owner.ReadLocalValue(_property) == DependencyProperty.UnsetValue)
{
// Read-only properties must be set using the Key
if (_property.ReadOnly)
{
_owner.SetValue(_property.DependencyPropertyKey, _collection);
}
else
{
_owner.SetValue(_property, _collection);
}
}
// Unhook the change handler because we're finsihed promoting
_collection.CollectionChanged -= OnDefaultValueChanged;
}
private readonly DependencyObject _owner;
private readonly DependencyProperty _property;
private readonly ObservableCollection _collection;
}
private ObservableCollection _default;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: DefaultvalueFactory for ObservableCollection
//
// History:
// 2009/24/07 : [....] - Created.
//
//---------------------------------------------------------------------------
using MS.Internal.WindowsBase;
using System;
using System.Diagnostics;
using System.Windows;
using System.Collections.ObjectModel; // ObservableCollection
using System.Collections.Specialized; // NotifyCollectionChangedEventHandler
namespace MS.Internal
{
//
// ObservableCollectionDefaultValueFactory is a DefaultValueFactory implementation which will
// promote the default value to a local value if the collection is modified.
//
[FriendAccessAllowed] // built into Base, used by Framework
internal class ObservableCollectionDefaultValueFactory : DefaultValueFactory
{
internal ObservableCollectionDefaultValueFactory()
{
_default = new ObservableCollection();
}
///
/// This is used for Sealed objects. ObservableCollections are inherently mutable, so they shouldn't
/// be used with sealed objects. The PropertyDescriptor calls this, so we'll just return the same empty collection.
///
internal override object DefaultValue
{
get
{
return _default;
}
}
///
///
internal override object CreateDefaultValue(DependencyObject owner, DependencyProperty property)
{
Debug.Assert(owner != null && property != null,
"It is the caller responsibility to ensure that owner and property are non-null.");
var result = new ObservableCollection();
// Wire up a ObservableCollectionDefaultPromoter to observe the default value we
// just created and automatically promote it to local if it is modified.
// NOTE: We do not holding a reference to this because it should have the same lifetime as
// the collection. It will not be immediately GC'ed because it hooks the collections change event.
new ObservableCollectionDefaultPromoter(owner, property, result);
return result;
}
///
/// The ObservableCollectionDefaultPromoter observes the mutable defaults we hand out
/// for changed events. If the default is ever modified this class will
/// promote it to a local value by writing it to the local store and
/// clear the cached default value so we will generate a new default
/// the next time the property system is asked for one.
///
private class ObservableCollectionDefaultPromoter
{
internal ObservableCollectionDefaultPromoter(DependencyObject owner, DependencyProperty property, ObservableCollection collection)
{
Debug.Assert(owner != null && property != null,
"Caller is responsible for ensuring that owner and property are non-null.");
Debug.Assert(property.GetMetadata(owner.DependencyObjectType).UsingDefaultValueFactory,
"How did we end up observing a mutable if we were not registered for the factory pattern?");
// We hang on to the property and owner so we can write the default
// value back to the local store if it changes. See also
// OnDefaultValueChanged.
_owner = owner;
_property = property;
_collection = collection;
_collection.CollectionChanged += OnDefaultValueChanged;
}
internal void OnDefaultValueChanged(object sender, NotifyCollectionChangedEventArgs e)
{
PropertyMetadata metadata = _property.GetMetadata(_owner.DependencyObjectType);
// Remove this value from the DefaultValue cache so we stop
// handing it out as the default value now that it has changed.
metadata.ClearCachedDefaultValue(_owner, _property);
// If someone else hasn't already written a local value,
// promote the default value to local.
if (_owner.ReadLocalValue(_property) == DependencyProperty.UnsetValue)
{
// Read-only properties must be set using the Key
if (_property.ReadOnly)
{
_owner.SetValue(_property.DependencyPropertyKey, _collection);
}
else
{
_owner.SetValue(_property, _collection);
}
}
// Unhook the change handler because we're finsihed promoting
_collection.CollectionChanged -= OnDefaultValueChanged;
}
private readonly DependencyObject _owner;
private readonly DependencyProperty _property;
private readonly ObservableCollection _collection;
}
private ObservableCollection _default;
}
}
// 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
- RuleSetReference.cs
- NamespaceDecl.cs
- FormViewPagerRow.cs
- StatusBarPanelClickEvent.cs
- Point3DValueSerializer.cs
- EditableLabelControl.cs
- TreeViewBindingsEditorForm.cs
- LateBoundBitmapDecoder.cs
- XmlSerializerFactory.cs
- FileDialogCustomPlacesCollection.cs
- FormViewDeleteEventArgs.cs
- XmlDesigner.cs
- SessionEndingEventArgs.cs
- SoapUnknownHeader.cs
- SchemaTableOptionalColumn.cs
- ProcessHostMapPath.cs
- ProcessHostFactoryHelper.cs
- HotSpotCollection.cs
- EnumType.cs
- FillErrorEventArgs.cs
- ThemeableAttribute.cs
- CustomValidator.cs
- ActivityCodeDomSerializationManager.cs
- SafeNativeMethods.cs
- tibetanshape.cs
- TextTreeTextBlock.cs
- PathGeometry.cs
- login.cs
- WithStatement.cs
- XPathArrayIterator.cs
- UnsafeNativeMethods.cs
- CodeExpressionCollection.cs
- IdentitySection.cs
- GroupPartitionExpr.cs
- HttpValueCollection.cs
- CopyOnWriteList.cs
- HttpWebResponse.cs
- StrokeNodeOperations2.cs
- TypefaceMap.cs
- EmptyStringExpandableObjectConverter.cs
- GridViewSelectEventArgs.cs
- XmlParserContext.cs
- Transform3D.cs
- sqlstateclientmanager.cs
- BulletDecorator.cs
- SoapSchemaExporter.cs
- ListViewInsertedEventArgs.cs
- LOSFormatter.cs
- ContentDisposition.cs
- ObjectToModelValueConverter.cs
- ObjectQuery_EntitySqlExtensions.cs
- AnimationTimeline.cs
- VScrollBar.cs
- TypeDependencyAttribute.cs
- WindowsFormsHostPropertyMap.cs
- CommonDialog.cs
- HashCodeCombiner.cs
- DataGridViewCellStyleConverter.cs
- StringConverter.cs
- XmlReflectionMember.cs
- AssemblyAttributes.cs
- DataGridViewAccessibleObject.cs
- WindowsTab.cs
- WebPartTracker.cs
- ExpressionConverter.cs
- QilTernary.cs
- oledbmetadatacollectionnames.cs
- ExtensionSimplifierMarkupObject.cs
- PasswordPropertyTextAttribute.cs
- SQLByte.cs
- XmlSchemaSimpleTypeRestriction.cs
- OdbcException.cs
- SoapMessage.cs
- CacheVirtualItemsEvent.cs
- SqlCharStream.cs
- MsmqBindingMonitor.cs
- BrowserInteropHelper.cs
- BindingListCollectionView.cs
- EditorOptionAttribute.cs
- EndpointIdentityConverter.cs
- CodeExpressionRuleDeclaration.cs
- ResourceContainer.cs
- WmlLabelAdapter.cs
- ResourceReader.cs
- ExpressionVisitor.cs
- Matrix.cs
- CapiSymmetricAlgorithm.cs
- IdnElement.cs
- TreeNodeBinding.cs
- httpserverutility.cs
- DataGridSortCommandEventArgs.cs
- TransformerConfigurationWizardBase.cs
- SchemaLookupTable.cs
- compensatingcollection.cs
- ControlCachePolicy.cs
- XmlConverter.cs
- GlyphingCache.cs
- CompoundFileStreamReference.cs
- GPPOINT.cs
- WorkflowRuntime.cs