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
- AttributeTable.cs
- Exceptions.cs
- State.cs
- ReturnEventArgs.cs
- AesManaged.cs
- ScaleTransform3D.cs
- DropDownButton.cs
- ParamArrayAttribute.cs
- SequenceRange.cs
- Random.cs
- EmbeddedMailObjectCollectionEditor.cs
- InsufficientMemoryException.cs
- InstancePersistence.cs
- ObjectToIdCache.cs
- DataControlFieldHeaderCell.cs
- SecurityKeyIdentifierClause.cs
- ConstNode.cs
- TdsParserHelperClasses.cs
- TimeSpanConverter.cs
- DataFormats.cs
- MasterPageCodeDomTreeGenerator.cs
- ReferencedAssembly.cs
- Int16Storage.cs
- GeneralTransformGroup.cs
- ValidationPropertyAttribute.cs
- KeyTime.cs
- XmlNamespaceMappingCollection.cs
- UInt64Converter.cs
- HttpSessionStateWrapper.cs
- ToolStripHighContrastRenderer.cs
- RoutingEndpointTrait.cs
- RecordsAffectedEventArgs.cs
- SqlDataSourceSelectingEventArgs.cs
- DeflateEmulationStream.cs
- WasAdminWrapper.cs
- CmsInterop.cs
- XsltConvert.cs
- AsyncResult.cs
- ValueUnavailableException.cs
- TagMapInfo.cs
- QuaternionAnimation.cs
- Transform.cs
- TextEditor.cs
- IsolatedStorageFileStream.cs
- Base64Encoder.cs
- CollectionViewGroupInternal.cs
- TitleStyle.cs
- DateTimeValueSerializerContext.cs
- XPathNodePointer.cs
- TempFiles.cs
- SecurityDescriptor.cs
- ButtonFlatAdapter.cs
- FrameworkObject.cs
- Compiler.cs
- SiteMapProvider.cs
- AttachedPropertyBrowsableForTypeAttribute.cs
- Trace.cs
- AssertFilter.cs
- RectangleGeometry.cs
- TextSelectionProcessor.cs
- DropDownButton.cs
- MainMenu.cs
- __Filters.cs
- HorizontalAlignConverter.cs
- Main.cs
- TextRunCacheImp.cs
- DataObjectPastingEventArgs.cs
- StickyNoteContentControl.cs
- listviewsubitemcollectioneditor.cs
- IListConverters.cs
- ExpressionNormalizer.cs
- StorageInfo.cs
- TemplateControl.cs
- BlobPersonalizationState.cs
- GeometryModel3D.cs
- Button.cs
- DoubleCollectionValueSerializer.cs
- SoapRpcServiceAttribute.cs
- LinqDataSource.cs
- StateWorkerRequest.cs
- FrameworkTemplate.cs
- StylusDownEventArgs.cs
- ExtendedProtectionPolicyTypeConverter.cs
- AddingNewEventArgs.cs
- Grid.cs
- DayRenderEvent.cs
- XmlDictionaryReader.cs
- HostedNamedPipeTransportManager.cs
- PhysicalOps.cs
- RelationshipNavigation.cs
- Renderer.cs
- AliasedSlot.cs
- UserNamePasswordValidationMode.cs
- WindowsProgressbar.cs
- AttachmentService.cs
- TypeInfo.cs
- HotCommands.cs
- GlyphRunDrawing.cs
- XmlAtomicValue.cs