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
- ItemCheckedEvent.cs
- BackoffTimeoutHelper.cs
- ImageDrawing.cs
- OutKeywords.cs
- RightsManagementEncryptionTransform.cs
- EtwTrackingBehavior.cs
- PkcsMisc.cs
- AutoResizedEvent.cs
- DesignerGeometryHelper.cs
- PersonalizationStateInfoCollection.cs
- SessionState.cs
- XsdDuration.cs
- TransactionFilter.cs
- CheckBoxFlatAdapter.cs
- AssemblyEvidenceFactory.cs
- ExceptionWrapper.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- EmptyReadOnlyDictionaryInternal.cs
- Trace.cs
- DictionaryContent.cs
- MobileControlDesigner.cs
- SqlTriggerAttribute.cs
- XslAst.cs
- ExternalDataExchangeService.cs
- FrameworkReadOnlyPropertyMetadata.cs
- DbConnectionStringBuilder.cs
- XmlRawWriter.cs
- OracleTransaction.cs
- SafeLocalAllocation.cs
- WorkflowExecutor.cs
- PointKeyFrameCollection.cs
- ZipIOExtraFieldElement.cs
- DBDataPermission.cs
- Parsers.cs
- StreamInfo.cs
- EntityDataSourceEntityTypeFilterItem.cs
- PointCollection.cs
- XamlPointCollectionSerializer.cs
- KeyValuePair.cs
- SqlWebEventProvider.cs
- StrongNameKeyPair.cs
- QilPatternFactory.cs
- TabControl.cs
- EdmFunctions.cs
- DbDataReader.cs
- FieldTemplateUserControl.cs
- HtmlValidationSummaryAdapter.cs
- BindingMemberInfo.cs
- DbProviderFactory.cs
- Switch.cs
- CacheRequest.cs
- DSASignatureFormatter.cs
- DataExpression.cs
- BinaryMethodMessage.cs
- ImageAttributes.cs
- Dictionary.cs
- TraceXPathNavigator.cs
- RepeatButton.cs
- ThemeDirectoryCompiler.cs
- UTF7Encoding.cs
- DateTimeStorage.cs
- CopyCodeAction.cs
- TypeLibConverter.cs
- ExceptionWrapper.cs
- StreamMarshaler.cs
- DetailsViewInsertedEventArgs.cs
- Renderer.cs
- WebPartHeaderCloseVerb.cs
- IncrementalReadDecoders.cs
- UniqueConstraint.cs
- LookupBindingPropertiesAttribute.cs
- ColorConverter.cs
- CrossContextChannel.cs
- ChannelServices.cs
- DataSourceXmlTextReader.cs
- TemplateControlParser.cs
- TypeConverters.cs
- InvokeProviderWrapper.cs
- ControlLocalizer.cs
- TypeDescriptorContext.cs
- UnicastIPAddressInformationCollection.cs
- WindowClosedEventArgs.cs
- String.cs
- SendMailErrorEventArgs.cs
- DataTemplateSelector.cs
- KeyboardEventArgs.cs
- ChangeNode.cs
- CatalogPartCollection.cs
- X509Certificate2Collection.cs
- TextOutput.cs
- XmlQuerySequence.cs
- ContainerSelectorActiveEvent.cs
- BinaryConverter.cs
- CustomErrorsSectionWrapper.cs
- ErrorTableItemStyle.cs
- TypeNameConverter.cs
- MarkupObject.cs
- ResourceManager.cs
- ClientProxyGenerator.cs
- SmtpNetworkElement.cs