/// <inheritdoc/> public override bool CanAttach(INodePresenter nodePresenter) { // We are in a collection or dictionary... var collectionNode = (nodePresenter as ItemNodePresenter)?.OwnerCollection; var collectionDescriptor = collectionNode?.Descriptor as CollectionDescriptor; var dictionaryDescriptor = collectionNode?.Descriptor as DictionaryDescriptor; if (collectionDescriptor == null && dictionaryDescriptor == null) return false; // ... that is not read-only... var memberCollection = (collectionNode as MemberNodePresenter)?.MemberAttributes.OfType<MemberCollectionAttribute>().FirstOrDefault() ?? collectionNode.Descriptor.Attributes.OfType<MemberCollectionAttribute>().FirstOrDefault(); if (memberCollection?.ReadOnly == true) return false; // ... and supports remove... if (collectionDescriptor != null) { var elementType = collectionDescriptor.ElementType; // We also add the same conditions that for AddNewItem return collectionDescriptor.HasRemoveAt && AddNewItemCommand.CanAdd(elementType); } // TODO: add a HasRemove in the dictionary descriptor and test it! return true; }
/// <inheritdoc/> public override bool CanAttach(INodePresenter nodePresenter) { // We are in a dictionary... var dictionaryDescriptor = nodePresenter.Descriptor as DictionaryDescriptor; if (dictionaryDescriptor == null) { return(false); } // ... that is not read-only... var memberCollection = (nodePresenter as MemberNodePresenter)?.MemberAttributes.OfType <MemberCollectionAttribute>().FirstOrDefault() ?? nodePresenter.Descriptor.Attributes.OfType <MemberCollectionAttribute>().FirstOrDefault(); if (memberCollection?.ReadOnly == true) { return(false); } // ... can construct key type... if (!AddNewItemCommand.CanConstruct(dictionaryDescriptor.KeyType)) { return(false); } // ... and can construct value type var elementType = dictionaryDescriptor.ValueType; return(AddNewItemCommand.CanAdd(elementType)); }
/// <inheritdoc/> public override bool CanAttach(INodePresenter nodePresenter) { // We are in a dictionary if (!(nodePresenter.Descriptor is DictionaryDescriptor dictionaryDescriptor)) { return(false); } // It is not read-only var memberCollection = (nodePresenter as MemberNodePresenter)?.MemberAttributes .OfType <MemberCollectionAttribute>() .FirstOrDefault() ?? nodePresenter.Descriptor.Attributes .OfType <MemberCollectionAttribute>() .FirstOrDefault(); if (memberCollection?.ReadOnly == true) { return(false); } // It can construct the key type if (!AddNewItemCommand.CanConstruct(dictionaryDescriptor.KeyType)) { return(false); } // And it can construct the value type var elementType = dictionaryDescriptor.ValueType; return(AddNewItemCommand.CanAdd(elementType)); }