Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Presentation / System / Activities / Presentation / Model / ModelItemImpl.cs / 1305376 / ModelItemImpl.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities.Presentation.Model { using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Windows; using System.Windows.Markup; using System.Activities.Presentation.Services; using System.Activities.Presentation.View; using System.Linq; using System.Runtime; using System.Dynamic; using System.Reflection; // This class provides the View facing ModelItem implementation, this works with the ModelTreeManager // and keeps the xaml up to date by intercepting every change to the model properties. class ModelItemImpl : ModelItem, IModelTreeItem, ICustomTypeDescriptor, IDynamicMetaObjectProvider { ModelProperty contentProperty; object instance; Type itemType; DictionarymodelPropertyStore; ModelTreeManager modelTreeManager; ModelProperty nameProperty; List parents; ModelPropertyCollectionImpl properties; List sources; List subTreeNodesThatNeedBackLinkPatching; DependencyObject view; public ModelItemImpl(ModelTreeManager modelTreeManager, Type itemType, object instance, ModelItem parent) { this.itemType = itemType; this.instance = instance; this.modelTreeManager = modelTreeManager; this.parents = new List (1); this.sources = new List (1); if (parent != null) { this.parents.Add(parent); } this.modelPropertyStore = new Dictionary (); this.subTreeNodesThatNeedBackLinkPatching = new List (); } public override event PropertyChangedEventHandler PropertyChanged; public override global::System.ComponentModel.AttributeCollection Attributes { get { return TypeDescriptor.GetAttributes(itemType); } } public override ModelProperty Content { get { if (this.contentProperty == null) { Fx.Assert(this.instance != null, "instance should not be null"); ContentPropertyAttribute contentAttribute = TypeDescriptor.GetAttributes(this.instance)[typeof(ContentPropertyAttribute)] as ContentPropertyAttribute; if (contentAttribute != null && !String.IsNullOrEmpty(contentAttribute.Name)) { this.contentProperty = this.Properties.Find(contentAttribute.Name); } } return contentProperty; } } public override Type ItemType { get { return this.itemType; } } public ModelItem ModelItem { get { return this; } } public override string Name { get { string name = null; if ((this.NameProperty != null) && (this.NameProperty.Value != null)) { name = (string)this.NameProperty.Value.GetCurrentValue(); } return name; } set { if (this.NameProperty != null) { this.NameProperty.SetValue(value); } } } public override ModelItem Parent { get { return (this.Parents.Count() > 0) ? this.Parents.First() : null; } } public override ModelPropertyCollection Properties { get { if (this.properties == null) { properties = new ModelPropertyCollectionImpl(this); } return properties; } } public override ModelItem Root { get { return this.modelTreeManager.Root; } } // This holds a reference to the modelproperty that is currently holding this ModelItem. public override ModelProperty Source { get { return (this.sources.Count > 0) ? this.sources.First() : null; } } public override DependencyObject View { get { return this.view; } } public override IEnumerable Parents { get { return this.parents.Concat( from source in this.sources select source.Parent); } } public override IEnumerable Sources { get { return this.sources; } } protected ModelProperty NameProperty { get { if (this.nameProperty == null) { Fx.Assert(this.instance != null, "instance should not be null"); RuntimeNamePropertyAttribute runtimeNamePropertyAttribute = TypeDescriptor.GetAttributes(this.instance)[typeof(RuntimeNamePropertyAttribute)] as RuntimeNamePropertyAttribute; if (runtimeNamePropertyAttribute != null && !String.IsNullOrEmpty(runtimeNamePropertyAttribute.Name)) { this.nameProperty = this.Properties.Find(runtimeNamePropertyAttribute.Name); } } return nameProperty; } } Dictionary IModelTreeItem.ModelPropertyStore { get { return modelPropertyStore; } } ModelTreeManager IModelTreeItem.ModelTreeManager { get { return modelTreeManager; } } void IModelTreeItem.SetCurrentView(DependencyObject view) { this.view = view; } public override ModelEditingScope BeginEdit(string description) { return this.modelTreeManager.CreateEditingScope(description); } public override ModelEditingScope BeginEdit() { return this.BeginEdit(null); } public override object GetCurrentValue() { return this.instance; } void IModelTreeItem.OnPropertyChanged(string propertyName) { this.OnPropertyChanged(propertyName); } void IModelTreeItem.SetParent(ModelItem dataModelItem) { if (dataModelItem != null && !this.parents.Contains(dataModelItem)) { this.parents.Add(dataModelItem); } } void IModelTreeItem.SetSource(ModelProperty property) { if (!this.sources.Contains(property)) { // also check if the same parent.property is in the list as a different instance of oldModelProperty ModelProperty foundProperty = this.sources.Find((modelProperty) => modelProperty.Name.Equals(property.Name) && property.Parent == modelProperty.Parent); if (foundProperty == null) { this.sources.Add(property); } } } IList IModelTreeItem.SubNodesThatNeedBackLinkUpdate { get { return this.subTreeNodesThatNeedBackLinkPatching; } } #region ICustomTypeDescriptor Members AttributeCollection ICustomTypeDescriptor.GetAttributes() { return this.Attributes; } string ICustomTypeDescriptor.GetClassName() { return TypeDescriptor.GetClassName(this); } string ICustomTypeDescriptor.GetComponentName() { return TypeDescriptor.GetComponentName(this); } TypeConverter ICustomTypeDescriptor.GetConverter() { return ModelUtilities.GetConverter(this); } EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { // we dont support events; return null; } PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return ModelUtilities.GetDefaultProperty(this); } object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { // we dont support editors return null; } EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { // we dont support events; return null; } EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { // we dont support events; return null; } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) { return ModelUtilities.WrapProperties(this); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { // get model properties List properties = new List (); foreach (PropertyDescriptor modelPropertyDescriptor in ModelUtilities.WrapProperties(this)) { properties.Add(modelPropertyDescriptor); } // try to see if there are pseudo builtin properties for this type. AttachedPropertiesService AttachedPropertiesService = this.modelTreeManager.Context.Services.GetService (); if (AttachedPropertiesService != null) { var nonBrowsableAttachedProperties = from attachedProperty in AttachedPropertiesService.GetAttachedProperties(this.itemType) where !attachedProperty.IsBrowsable select attachedProperty; foreach (AttachedProperty AttachedProperty in nonBrowsableAttachedProperties) { properties.Add(new AttachedPropertyDescriptor(AttachedProperty, this)); } } return new PropertyDescriptorCollection(properties.ToArray(), true); } object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return this; } #endregion void IModelTreeItem.RemoveParent(ModelItem oldParent) { if (this.parents.Contains(oldParent)) { this.parents.Remove(oldParent); } } void IModelTreeItem.RemoveSource(ModelProperty oldModelProperty) { if (this.sources.Contains(oldModelProperty)) { this.sources.Remove(oldModelProperty); } else { // also check if the same parent.property is in the list as a different instance of oldModelProperty ModelProperty foundProperty = this.sources.Find((modelProperty) => modelProperty.Name.Equals(oldModelProperty.Name) && modelProperty.Parent == oldModelProperty.Parent); if (foundProperty != null) { this.sources.Remove(foundProperty); } } } protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression parameter) { return new ModelItemMetaObject(parameter,this); } public object SetPropertyValue(string propertyName, object val) { ModelProperty modelProperty = this.Properties.Find(propertyName); if (modelProperty != null) { modelProperty.SetValue(val); } else { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this)[propertyName]; if (descriptor != null) { descriptor.SetValue(this, val); } } return GetPropertyValue(propertyName); } public object GetPropertyValue(string propertyName) { ModelProperty modelProperty = this.Properties.Find(propertyName); object value = null; if (modelProperty != null) { ModelItem valueModelitem = modelProperty.Value; if (valueModelitem == null) { value = null; } else { Type itemType = valueModelitem.ItemType; if (itemType.IsPrimitive || itemType.IsEnum || itemType.Equals(typeof(String))) { value = valueModelitem.GetCurrentValue(); } else { value = valueModelitem; } } } else { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this)[propertyName]; if (descriptor != null) { value = descriptor.GetValue(this); } } return value; } class ModelItemMetaObject : System.Dynamic.DynamicMetaObject { MethodInfo getPropertyValueMethodInfo = typeof(ModelItemImpl).GetMethod("GetPropertyValue"); MethodInfo setPropertyValueMethodInfo = typeof(ModelItemImpl).GetMethod("SetPropertyValue"); public ModelItemMetaObject(System.Linq.Expressions.Expression parameter, ModelItemImpl target) : base(parameter, BindingRestrictions.Empty,target) { } public override DynamicMetaObject BindGetMember(GetMemberBinder binder) { System.Linq.Expressions.Expression s = System.Linq.Expressions.Expression.Convert(this.Expression, typeof(ModelItemImpl)); System.Linq.Expressions.Expression value = System.Linq.Expressions.Expression.Call(s, getPropertyValueMethodInfo, System.Linq.Expressions.Expression.Constant(binder.Name)); return new DynamicMetaObject(value, BindingRestrictions.GetTypeRestriction(this.Expression, this.LimitType)); } public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value) { System.Linq.Expressions.Expression s = System.Linq.Expressions.Expression.Convert(this.Expression, typeof(ModelItemImpl)); System.Linq.Expressions.Expression objectValue = System.Linq.Expressions.Expression.Convert(value.Expression, typeof(object)); System.Linq.Expressions.Expression valueExp = System.Linq.Expressions.Expression.Call(s, setPropertyValueMethodInfo, System.Linq.Expressions.Expression.Constant(binder.Name), objectValue); return new DynamicMetaObject(valueExp, BindingRestrictions.GetTypeRestriction(this.Expression, this.LimitType)); } } } } // 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
- DataTemplateKey.cs
- ListViewEditEventArgs.cs
- PointHitTestResult.cs
- BulletedListDesigner.cs
- RC2.cs
- BuildProvidersCompiler.cs
- OlePropertyStructs.cs
- LoginUtil.cs
- ZipIOExtraFieldZip64Element.cs
- WebFormDesignerActionService.cs
- CodeDomConfigurationHandler.cs
- TemplateKeyConverter.cs
- SqlDependency.cs
- SubqueryTrackingVisitor.cs
- DetailsViewInsertedEventArgs.cs
- EventLogInformation.cs
- CodeExpressionCollection.cs
- LayoutDump.cs
- SqlDataSourceEnumerator.cs
- MimeMapping.cs
- AnnotationDocumentPaginator.cs
- ItemCollection.cs
- SiteOfOriginContainer.cs
- OuterGlowBitmapEffect.cs
- WmlMobileTextWriter.cs
- FilteredDataSetHelper.cs
- FixedPage.cs
- BookmarkCallbackWrapper.cs
- MenuItemStyleCollectionEditor.cs
- NumberEdit.cs
- HwndSubclass.cs
- ReferencedCollectionType.cs
- TargetConverter.cs
- SingleKeyFrameCollection.cs
- GuidelineSet.cs
- SqlDependency.cs
- GridViewRowCollection.cs
- DataRowView.cs
- SqlRemoveConstantOrderBy.cs
- FileStream.cs
- MDIControlStrip.cs
- SafeTokenHandle.cs
- XmlSchemaInfo.cs
- UriWriter.cs
- DbConnectionPoolGroupProviderInfo.cs
- WebPageTraceListener.cs
- SocketAddress.cs
- TextContainerChangeEventArgs.cs
- DependencyPropertyDescriptor.cs
- sitestring.cs
- MutableAssemblyCacheEntry.cs
- TagNameToTypeMapper.cs
- VerificationException.cs
- AccessDataSource.cs
- TextSerializer.cs
- StsCommunicationException.cs
- AutoCompleteStringCollection.cs
- GenericXmlSecurityToken.cs
- TriggerBase.cs
- ThreadStateException.cs
- DocumentViewerBaseAutomationPeer.cs
- XmlUtf8RawTextWriter.cs
- ReceiveContextCollection.cs
- TextSpan.cs
- XmlArrayItemAttributes.cs
- OutputScope.cs
- NamedElement.cs
- ClipboardProcessor.cs
- CompareValidator.cs
- WrappedIUnknown.cs
- InArgumentConverter.cs
- FloatUtil.cs
- RadioButtonBaseAdapter.cs
- TrustSection.cs
- ColorPalette.cs
- CommonDialog.cs
- XmlSecureResolver.cs
- Interlocked.cs
- ToolStripSystemRenderer.cs
- WmlValidatorAdapter.cs
- Event.cs
- WebPartEditVerb.cs
- AdCreatedEventArgs.cs
- BooleanKeyFrameCollection.cs
- CopyCodeAction.cs
- RNGCryptoServiceProvider.cs
- UnSafeCharBuffer.cs
- EtwTrace.cs
- CompositeDuplexElement.cs
- StateManagedCollection.cs
- MembershipSection.cs
- HtmlTableCellCollection.cs
- CategoryAttribute.cs
- SoapHeader.cs
- PolyBezierSegment.cs
- SafeIUnknown.cs
- DataGridViewTextBoxCell.cs
- InvariantComparer.cs
- _SslState.cs
- UIElementHelper.cs