Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Base / MS / Internal / FreezableDefaultValueFactory.cs / 1 / FreezableDefaultValueFactory.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description: DefaultvalueFactory for Freezables
//
// History:
// 2005/11/08 : jordanpa - Created from PresentationCore's old
// MutableDefaultPropertyMetadata
//
//---------------------------------------------------------------------------
using MS.Internal.WindowsBase;
using System;
using System.Diagnostics;
using System.Windows;
namespace MS.Internal
{
//
// FreezableDefaultValueFactory is a DefaultValueFactory implementation which
// is inserted by the property system for any DP registered with a default
// value of type Freezable. The user’s given default value is frozen and
// used as a template to create unfrozen copies on a per DP per DO basis. If
// the default value is modified it is automatically promoted from default to
// local.
//
[FriendAccessAllowed] // built into Base, used by Core + Framework
internal class FreezableDefaultValueFactory : DefaultValueFactory
{
///
/// Stores a frozen copy of defaultValue
///
internal FreezableDefaultValueFactory(Freezable defaultValue)
{
Debug.Assert(defaultValue != null,
"Null can not be made mutable. Do not use FreezableDefaultValueFactory.");
Debug.Assert(defaultValue.CanFreeze,
"The defaultValue prototype must be freezable.");
_defaultValuePrototype = defaultValue.GetAsFrozen();
}
///
/// Returns our frozen sentinel
///
internal override object DefaultValue
{
get
{
Debug.Assert(_defaultValuePrototype.IsFrozen);
return _defaultValuePrototype;
}
}
///
/// If the DO is frozen, we'll return our frozen sentinel. Otherwise we'll make
/// an unfrozen copy.
///
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.");
Freezable result = _defaultValuePrototype;
Freezable ownerFreezable = owner as Freezable;
// If the owner is frozen, just return the frozen prototype.
if (ownerFreezable != null && ownerFreezable.IsFrozen)
{
return result;
}
result = _defaultValuePrototype.Clone();
// Wire up a FreezableDefaultPromoter to observe the default value we
// just created and automatically promote it to local if it is modified.
FreezableDefaultPromoter promoter = new FreezableDefaultPromoter(owner, property);
promoter.SetFreezableDefaultValue(result);
result.Changed += promoter.OnDefaultValueChanged;
return result;
}
// This is the prototype that CreateDefaultValue copies to create the
// mutable default value for this property. See also the ctor.
private readonly Freezable _defaultValuePrototype;
///
/// The FreezableDefaultPromoter 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 FreezableDefaultPromoter
{
internal FreezableDefaultPromoter(DependencyObject owner, DependencyProperty property)
{
Debug.Assert(owner != null && property != null,
"Caller is responsible for ensuring that owner and property are non-null.");
Debug.Assert(!(owner is Freezable) || !((Freezable)owner).IsFrozen,
"We should not be observing mutables on a frozen owner.");
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;
}
internal void OnDefaultValueChanged(object sender, EventArgs e)
{
Debug.Assert(_mutableDefaultValue != null,
"Promoter's creator should have called SetFreezableDefaultValue.");
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);
// Since Changed is raised when the user freezes the default
// value, we need to check before removing our handler.
// (If the value is frozen, it will remove it's own handlers.)
if (!_mutableDefaultValue.IsFrozen)
{
_mutableDefaultValue.Changed -= OnDefaultValueChanged;
}
// If someone else hasn't already written a local local value,
// promote the default value to local.
if (_owner.ReadLocalValue(_property) == DependencyProperty.UnsetValue)
{
_owner.SetMutableDefaultValue(_property, _mutableDefaultValue);
}
}
private readonly DependencyObject _owner;
private readonly DependencyProperty _property;
#region DefaultValue
// The creator of a FreezableDefaultValuePromoter should call this method
// so that we can verify that the changed sender is the mutable default
// value we handed out.
internal void SetFreezableDefaultValue(Freezable mutableDefaultValue)
{
_mutableDefaultValue = mutableDefaultValue;
}
private Freezable _mutableDefaultValue;
#endregion DefaultValue
}
}
}
// 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 Freezables
//
// History:
// 2005/11/08 : jordanpa - Created from PresentationCore's old
// MutableDefaultPropertyMetadata
//
//---------------------------------------------------------------------------
using MS.Internal.WindowsBase;
using System;
using System.Diagnostics;
using System.Windows;
namespace MS.Internal
{
//
// FreezableDefaultValueFactory is a DefaultValueFactory implementation which
// is inserted by the property system for any DP registered with a default
// value of type Freezable. The user’s given default value is frozen and
// used as a template to create unfrozen copies on a per DP per DO basis. If
// the default value is modified it is automatically promoted from default to
// local.
//
[FriendAccessAllowed] // built into Base, used by Core + Framework
internal class FreezableDefaultValueFactory : DefaultValueFactory
{
///
/// Stores a frozen copy of defaultValue
///
internal FreezableDefaultValueFactory(Freezable defaultValue)
{
Debug.Assert(defaultValue != null,
"Null can not be made mutable. Do not use FreezableDefaultValueFactory.");
Debug.Assert(defaultValue.CanFreeze,
"The defaultValue prototype must be freezable.");
_defaultValuePrototype = defaultValue.GetAsFrozen();
}
///
/// Returns our frozen sentinel
///
internal override object DefaultValue
{
get
{
Debug.Assert(_defaultValuePrototype.IsFrozen);
return _defaultValuePrototype;
}
}
///
/// If the DO is frozen, we'll return our frozen sentinel. Otherwise we'll make
/// an unfrozen copy.
///
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.");
Freezable result = _defaultValuePrototype;
Freezable ownerFreezable = owner as Freezable;
// If the owner is frozen, just return the frozen prototype.
if (ownerFreezable != null && ownerFreezable.IsFrozen)
{
return result;
}
result = _defaultValuePrototype.Clone();
// Wire up a FreezableDefaultPromoter to observe the default value we
// just created and automatically promote it to local if it is modified.
FreezableDefaultPromoter promoter = new FreezableDefaultPromoter(owner, property);
promoter.SetFreezableDefaultValue(result);
result.Changed += promoter.OnDefaultValueChanged;
return result;
}
// This is the prototype that CreateDefaultValue copies to create the
// mutable default value for this property. See also the ctor.
private readonly Freezable _defaultValuePrototype;
///
/// The FreezableDefaultPromoter 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 FreezableDefaultPromoter
{
internal FreezableDefaultPromoter(DependencyObject owner, DependencyProperty property)
{
Debug.Assert(owner != null && property != null,
"Caller is responsible for ensuring that owner and property are non-null.");
Debug.Assert(!(owner is Freezable) || !((Freezable)owner).IsFrozen,
"We should not be observing mutables on a frozen owner.");
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;
}
internal void OnDefaultValueChanged(object sender, EventArgs e)
{
Debug.Assert(_mutableDefaultValue != null,
"Promoter's creator should have called SetFreezableDefaultValue.");
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);
// Since Changed is raised when the user freezes the default
// value, we need to check before removing our handler.
// (If the value is frozen, it will remove it's own handlers.)
if (!_mutableDefaultValue.IsFrozen)
{
_mutableDefaultValue.Changed -= OnDefaultValueChanged;
}
// If someone else hasn't already written a local local value,
// promote the default value to local.
if (_owner.ReadLocalValue(_property) == DependencyProperty.UnsetValue)
{
_owner.SetMutableDefaultValue(_property, _mutableDefaultValue);
}
}
private readonly DependencyObject _owner;
private readonly DependencyProperty _property;
#region DefaultValue
// The creator of a FreezableDefaultValuePromoter should call this method
// so that we can verify that the changed sender is the mutable default
// value we handed out.
internal void SetFreezableDefaultValue(Freezable mutableDefaultValue)
{
_mutableDefaultValue = mutableDefaultValue;
}
private Freezable _mutableDefaultValue;
#endregion DefaultValue
}
}
}
// 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
- ContentFileHelper.cs
- DialogDivider.cs
- WebPartConnectionsCancelVerb.cs
- Accessible.cs
- MemberJoinTreeNode.cs
- Size3DConverter.cs
- NativeCppClassAttribute.cs
- CheckBox.cs
- TextBox.cs
- ACE.cs
- Unit.cs
- MailWebEventProvider.cs
- TimeoutValidationAttribute.cs
- ResizingMessageFilter.cs
- CompositeDataBoundControl.cs
- RegexMatch.cs
- TypeNameConverter.cs
- ServiceProviders.cs
- DataGridCommandEventArgs.cs
- XPathNodeList.cs
- CultureInfo.cs
- DataBindingCollectionEditor.cs
- MatrixAnimationUsingKeyFrames.cs
- Matrix3D.cs
- EmptyEnumerator.cs
- CachedTypeface.cs
- SafeProcessHandle.cs
- AttachedPropertyMethodSelector.cs
- CommandHelpers.cs
- XamlStyleSerializer.cs
- UnmanagedMemoryStreamWrapper.cs
- Dynamic.cs
- XmlDataSourceDesigner.cs
- ClassGenerator.cs
- ToolBarPanel.cs
- DataColumnChangeEvent.cs
- DocumentApplicationJournalEntryEventArgs.cs
- AsymmetricAlgorithm.cs
- ItemChangedEventArgs.cs
- SafeBitVector32.cs
- IgnorePropertiesAttribute.cs
- HostedHttpContext.cs
- ScrollPatternIdentifiers.cs
- ArgumentOutOfRangeException.cs
- SubqueryTrackingVisitor.cs
- TaskHelper.cs
- IntegerValidator.cs
- TypeDescriptorFilterService.cs
- SizeChangedEventArgs.cs
- TriggerActionCollection.cs
- TextSpanModifier.cs
- ConstraintConverter.cs
- ZipIOExtraField.cs
- TransformerInfo.cs
- FamilyMap.cs
- HiddenField.cs
- Help.cs
- TraceContext.cs
- SafeTokenHandle.cs
- NamespaceDecl.cs
- FontNameEditor.cs
- ConfigErrorGlyph.cs
- XPathPatternParser.cs
- ZoneButton.cs
- RadioButtonStandardAdapter.cs
- XmlEncoding.cs
- EnumUnknown.cs
- MenuCommand.cs
- ManagementInstaller.cs
- EncodedStreamFactory.cs
- PropertyOrder.cs
- ToolStripOverflowButton.cs
- TextDecoration.cs
- DrawingAttributes.cs
- MaxValueConverter.cs
- SQLInt32.cs
- GenericIdentity.cs
- storepermission.cs
- FreezableCollection.cs
- LongCountAggregationOperator.cs
- QilInvokeLateBound.cs
- SystemWebSectionGroup.cs
- MulticastIPAddressInformationCollection.cs
- COAUTHIDENTITY.cs
- TextTreeExtractElementUndoUnit.cs
- UniformGrid.cs
- IdentityReference.cs
- ConstNode.cs
- shaperfactory.cs
- CodeTypeReferenceSerializer.cs
- CodeDirectoryCompiler.cs
- WinEventQueueItem.cs
- TransportConfigurationTypeElementCollection.cs
- TableRow.cs
- DataSetFieldSchema.cs
- MultiAsyncResult.cs
- PathGeometry.cs
- EventHandlerList.cs
- WindowsListViewGroupHelper.cs
- HtmlFormParameterReader.cs