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
- IDReferencePropertyAttribute.cs
- Point3DValueSerializer.cs
- xmlfixedPageInfo.cs
- ReadOnlyNameValueCollection.cs
- Dynamic.cs
- SHA384Managed.cs
- StreamAsIStream.cs
- HttpCapabilitiesSectionHandler.cs
- CellParagraph.cs
- Tuple.cs
- tooltip.cs
- DataGridViewCheckBoxColumn.cs
- XmlSchemaException.cs
- Int64.cs
- remotingproxy.cs
- DataSet.cs
- WmlTextViewAdapter.cs
- HttpApplicationStateWrapper.cs
- ValidatedMobileControlConverter.cs
- VirtualDirectoryMappingCollection.cs
- AutomationPatternInfo.cs
- Transform3D.cs
- ApplicationDirectoryMembershipCondition.cs
- DataPointer.cs
- RtfToken.cs
- DataGridAddNewRow.cs
- DocumentGrid.cs
- TypeConverterHelper.cs
- ClassGenerator.cs
- AlphaSortedEnumConverter.cs
- Canvas.cs
- MemoryFailPoint.cs
- SchemaNamespaceManager.cs
- CacheAxisQuery.cs
- TransformerInfo.cs
- RelationshipFixer.cs
- DetailsViewDeleteEventArgs.cs
- Utils.cs
- DataRowView.cs
- EntityDataSourceContextDisposingEventArgs.cs
- IconBitmapDecoder.cs
- DataGridViewColumnConverter.cs
- DirectoryRootQuery.cs
- WebPartTransformer.cs
- CodeObject.cs
- BuiltInPermissionSets.cs
- CompleteWizardStep.cs
- DataKeyArray.cs
- AnnotationAuthorChangedEventArgs.cs
- ToolStripDropDownClosingEventArgs.cs
- InOutArgumentConverter.cs
- PartialArray.cs
- DPCustomTypeDescriptor.cs
- TTSEngineProxy.cs
- TreeViewDesigner.cs
- FormatPage.cs
- CustomExpression.cs
- JsonWriterDelegator.cs
- CompiledXpathExpr.cs
- DataTableNewRowEvent.cs
- Container.cs
- SqlOuterApplyReducer.cs
- FreezableDefaultValueFactory.cs
- DateTimeConverter.cs
- UIElementHelper.cs
- RemotingClientProxy.cs
- PageAsyncTaskManager.cs
- SqlPersonalizationProvider.cs
- TypeInitializationException.cs
- TextEditorTables.cs
- SemanticAnalyzer.cs
- XmlSigningNodeWriter.cs
- DefaultBinder.cs
- SignedXml.cs
- DataPagerCommandEventArgs.cs
- BitmapEffectDrawing.cs
- OutputCacheSettings.cs
- ResourceDescriptionAttribute.cs
- Expression.cs
- DataGridCaption.cs
- XslCompiledTransform.cs
- NameValueConfigurationElement.cs
- QueryConverter.cs
- ParseChildrenAsPropertiesAttribute.cs
- VSWCFServiceContractGenerator.cs
- DetailsViewModeEventArgs.cs
- CultureInfoConverter.cs
- StateMachineHelpers.cs
- SslStreamSecurityElement.cs
- BitmapDownload.cs
- TraceLog.cs
- CAGDesigner.cs
- BitHelper.cs
- OneOf.cs
- FloaterBaseParaClient.cs
- UriSection.cs
- JavascriptXmlWriterWrapper.cs
- SqlBuilder.cs
- OrderingExpression.cs
- FontNamesConverter.cs