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
- HtmlButton.cs
- FunctionQuery.cs
- SmtpTransport.cs
- EntityRecordInfo.cs
- UIElementHelper.cs
- OletxCommittableTransaction.cs
- SingleTagSectionHandler.cs
- SqlError.cs
- RtfControlWordInfo.cs
- PersonalizationState.cs
- MetadataItemSerializer.cs
- NullableLongSumAggregationOperator.cs
- FilteredReadOnlyMetadataCollection.cs
- ParserOptions.cs
- WebSysDescriptionAttribute.cs
- EncoderFallback.cs
- UICuesEvent.cs
- DesignerInterfaces.cs
- AppDomain.cs
- ImageConverter.cs
- ViewStateModeByIdAttribute.cs
- Soap.cs
- MimeBasePart.cs
- PointValueSerializer.cs
- TypeTypeConverter.cs
- SectionVisual.cs
- CmsUtils.cs
- WebPartConnectionsEventArgs.cs
- GenerateScriptTypeAttribute.cs
- BrowserDefinition.cs
- DesignerDataSourceView.cs
- ScrollPatternIdentifiers.cs
- PropertyDescriptorComparer.cs
- CodePrimitiveExpression.cs
- _ScatterGatherBuffers.cs
- NativeMethods.cs
- XamlStackWriter.cs
- ResourceIDHelper.cs
- BaseCodePageEncoding.cs
- StatusBarAutomationPeer.cs
- NavigatorInput.cs
- PowerModeChangedEventArgs.cs
- TaskHelper.cs
- QueryReaderSettings.cs
- PolyLineSegmentFigureLogic.cs
- CollectionBase.cs
- MethodBuilder.cs
- DefaultPropertyAttribute.cs
- ComponentConverter.cs
- ApplicationGesture.cs
- ServiceInfo.cs
- Utils.cs
- Rotation3DAnimationBase.cs
- EnumerableWrapperWeakToStrong.cs
- DesignerActionVerbItem.cs
- KnownColorTable.cs
- OfTypeExpression.cs
- XDRSchema.cs
- LiteralControl.cs
- SourceLineInfo.cs
- ELinqQueryState.cs
- RuntimeHelpers.cs
- StorageModelBuildProvider.cs
- MembershipUser.cs
- _PooledStream.cs
- PointHitTestResult.cs
- ScrollViewer.cs
- BooleanConverter.cs
- InternalCache.cs
- ContentPlaceHolder.cs
- EncryptedKeyHashIdentifierClause.cs
- DataGridViewRowCollection.cs
- Point3DCollection.cs
- FactoryGenerator.cs
- HtmlInputControl.cs
- CaretElement.cs
- ColumnTypeConverter.cs
- DesignerObjectListAdapter.cs
- FamilyCollection.cs
- SmtpTransport.cs
- MetabaseSettingsIis7.cs
- ColumnBinding.cs
- HwndKeyboardInputProvider.cs
- PowerStatus.cs
- StreamingContext.cs
- X509CertificateCollection.cs
- TriggerCollection.cs
- JoinQueryOperator.cs
- DependencyPropertyChangedEventArgs.cs
- ToolStripDropDownButton.cs
- ProfileEventArgs.cs
- ComponentEvent.cs
- BackgroundFormatInfo.cs
- OracleCommand.cs
- DbConnectionPool.cs
- ClientFormsIdentity.cs
- ListControlConvertEventArgs.cs
- AutomationProperties.cs
- CollectionViewProxy.cs
- GeneralTransform3DTo2D.cs