Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / ViewStateAttachedPropertyFeature.cs / 1305376 / ViewStateAttachedPropertyFeature.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation { using System.Activities.Presentation.Model; using System.Activities.Presentation.View; using System.Collections.Generic; using System.Runtime; abstract class ViewStateAttachedPropertyFeature : Feature { Type modelType; ViewStateService viewStateService; AttachedPropertiesService attachedPropertiesService; DictionaryattachedProperties = new Dictionary (); public sealed override void Initialize(EditingContext context, Type modelType) { this.modelType = modelType; context.Services.Subscribe (delegate(ViewStateService viewStateService) { this.viewStateService = viewStateService; viewStateService.ViewStateChanged += this.OnViewStateChanged; if (this.attachedPropertiesService != null) { RegisterAttachedProperties(); } }); context.Services.Subscribe (delegate(AttachedPropertiesService attachedPropertiesService) { this.attachedPropertiesService = attachedPropertiesService; if (this.viewStateService != null) { RegisterAttachedProperties(); } }); } protected abstract IEnumerable AttachedProperties { get; } internal void RegisterAttachedProperty (string propertyName, bool isBrowsable, T defaultValue) { AttachedProperty attachedProperty = new AttachedProperty { IsBrowsable = isBrowsable, Name = propertyName, OwnerType = modelType, Getter = (modelItem) => { T result = (T)viewStateService.RetrieveViewState(modelItem, propertyName); return result == null ? defaultValue : result; }, Setter = (modelItem, value) => { if (value == null || value.Equals(defaultValue)) { viewStateService.StoreViewStateWithUndo(modelItem, propertyName, null); } else { viewStateService.StoreViewStateWithUndo(modelItem, propertyName, value); } } }; attachedPropertiesService.AddProperty(attachedProperty); attachedProperties.Add(propertyName, attachedProperty); } void OnViewStateChanged(object sender, ViewStateChangedEventArgs e) { if (attachedProperties.ContainsKey(e.Key)) { // Checking is required to avoid infinite loop of ViewState -> AttachedProperty -> ... if ((e.NewValue == null && e.OldValue != null) || !e.NewValue.Equals(e.OldValue)) { attachedProperties[e.Key].SetValue(e.ParentModelItem, e.NewValue); } } } void RegisterAttachedProperties() { foreach (AttachedPropertyInfo attachedPropertyInfo in this.AttachedProperties) { attachedPropertyInfo.Register(this); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.Activities.Presentation { using System.Activities.Presentation.Model; using System.Activities.Presentation.View; using System.Collections.Generic; using System.Runtime; abstract class ViewStateAttachedPropertyFeature : Feature { Type modelType; ViewStateService viewStateService; AttachedPropertiesService attachedPropertiesService; Dictionary attachedProperties = new Dictionary (); public sealed override void Initialize(EditingContext context, Type modelType) { this.modelType = modelType; context.Services.Subscribe (delegate(ViewStateService viewStateService) { this.viewStateService = viewStateService; viewStateService.ViewStateChanged += this.OnViewStateChanged; if (this.attachedPropertiesService != null) { RegisterAttachedProperties(); } }); context.Services.Subscribe (delegate(AttachedPropertiesService attachedPropertiesService) { this.attachedPropertiesService = attachedPropertiesService; if (this.viewStateService != null) { RegisterAttachedProperties(); } }); } protected abstract IEnumerable AttachedProperties { get; } internal void RegisterAttachedProperty (string propertyName, bool isBrowsable, T defaultValue) { AttachedProperty attachedProperty = new AttachedProperty { IsBrowsable = isBrowsable, Name = propertyName, OwnerType = modelType, Getter = (modelItem) => { T result = (T)viewStateService.RetrieveViewState(modelItem, propertyName); return result == null ? defaultValue : result; }, Setter = (modelItem, value) => { if (value == null || value.Equals(defaultValue)) { viewStateService.StoreViewStateWithUndo(modelItem, propertyName, null); } else { viewStateService.StoreViewStateWithUndo(modelItem, propertyName, value); } } }; attachedPropertiesService.AddProperty(attachedProperty); attachedProperties.Add(propertyName, attachedProperty); } void OnViewStateChanged(object sender, ViewStateChangedEventArgs e) { if (attachedProperties.ContainsKey(e.Key)) { // Checking is required to avoid infinite loop of ViewState -> AttachedProperty -> ... if ((e.NewValue == null && e.OldValue != null) || !e.NewValue.Equals(e.OldValue)) { attachedProperties[e.Key].SetValue(e.ParentModelItem, e.NewValue); } } } void RegisterAttachedProperties() { foreach (AttachedPropertyInfo attachedPropertyInfo in this.AttachedProperties) { attachedPropertyInfo.Register(this); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ToolStripSystemRenderer.cs
- ActiveDocumentEvent.cs
- Visitors.cs
- ServicePointManagerElement.cs
- ModifierKeysValueSerializer.cs
- DataList.cs
- SettingsSection.cs
- TableItemStyle.cs
- ProgressBarRenderer.cs
- CharacterBufferReference.cs
- Model3DGroup.cs
- InvalidTimeZoneException.cs
- ProviderConnectionPoint.cs
- FindCompletedEventArgs.cs
- XmlAttributeProperties.cs
- BrowserTree.cs
- ObjectParameterCollection.cs
- MultiSelectRootGridEntry.cs
- TrackingProfileDeserializationException.cs
- Point3DIndependentAnimationStorage.cs
- XmlTextReaderImplHelpers.cs
- Variable.cs
- Menu.cs
- TagMapCollection.cs
- DataGridPagerStyle.cs
- MetadataHelper.cs
- HitTestParameters3D.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- HttpContext.cs
- CompoundFileDeflateTransform.cs
- ScrollBarRenderer.cs
- SchemaComplexType.cs
- AssemblyCollection.cs
- AdornerPresentationContext.cs
- Int32AnimationBase.cs
- PropertyPath.cs
- VScrollBar.cs
- StandardOleMarshalObject.cs
- WebConvert.cs
- Int32RectConverter.cs
- NamedPipeTransportBindingElement.cs
- SubqueryTrackingVisitor.cs
- Certificate.cs
- ToolStripDropTargetManager.cs
- _LoggingObject.cs
- HtmlShim.cs
- MediaPlayer.cs
- ValidationRuleCollection.cs
- _IPv4Address.cs
- ThicknessKeyFrameCollection.cs
- DragDeltaEventArgs.cs
- CatalogZoneDesigner.cs
- StateManagedCollection.cs
- ColorConverter.cs
- RawStylusSystemGestureInputReport.cs
- PropertyGridEditorPart.cs
- SqlCommandBuilder.cs
- FileClassifier.cs
- DiscreteKeyFrames.cs
- AutomationEventArgs.cs
- AdornerPresentationContext.cs
- TabOrder.cs
- Pen.cs
- SectionInformation.cs
- ParameterToken.cs
- CollectionViewGroup.cs
- UndirectedGraph.cs
- CompositionTarget.cs
- ToolStripPanelCell.cs
- MasterPage.cs
- DictionaryEntry.cs
- SerTrace.cs
- CollectionContainer.cs
- HwndSourceKeyboardInputSite.cs
- ClientSideQueueItem.cs
- HtmlTableCell.cs
- ValueUnavailableException.cs
- DataGridViewColumn.cs
- DataContractSerializerFaultFormatter.cs
- HttpContextServiceHost.cs
- Shape.cs
- AspCompat.cs
- DataRowIndexBuffer.cs
- TextBox.cs
- ViewKeyConstraint.cs
- PKCS1MaskGenerationMethod.cs
- Convert.cs
- HtmlShimManager.cs
- RtfNavigator.cs
- SID.cs
- UniqueSet.cs
- PropVariant.cs
- EqualityComparer.cs
- OutputCacheSection.cs
- ForEachAction.cs
- SpecialTypeDataContract.cs
- ToolStripSeparator.cs
- StorageMappingFragment.cs
- documentsequencetextcontainer.cs
- JsonCollectionDataContract.cs