@@ -285,32 +285,51 @@ XamlObject ParseObject(XmlElement element)
285285 void ParseObjectContent ( XamlObject obj , XmlElement element , XamlPropertyInfo defaultProperty , XamlTextValue initializeFromTextValueInsteadOfConstructor )
286286 {
287287 bool isDefaultValueSet = false ;
288- object defaultPropertyValue = null ;
289- XamlProperty defaultCollectionProperty = null ;
290288
291- if ( defaultProperty != null && defaultProperty . IsCollection && ! element . IsEmpty ) {
292- defaultPropertyValue = defaultProperty . GetValue ( obj . Instance ) ;
293- obj . AddProperty ( defaultCollectionProperty = new XamlProperty ( obj , defaultProperty ) ) ;
289+ XamlProperty collectionProperty = null ;
290+ object collectionInstance = null ;
291+ Type collectionType = null ;
292+ XmlElement collectionPropertyElement = null ;
293+ var elementChildNodes = GetNormalizedChildNodes ( element ) ;
294+
295+ if ( defaultProperty == null && obj . Instance != null && CollectionSupport . IsCollectionType ( obj . Instance . GetType ( ) ) ) {
296+ XamlObject parentObj = obj . ParentObject ;
297+ var parentElement = element . ParentNode ;
298+ XamlPropertyInfo propertyInfo = GetPropertyInfo ( settings . TypeFinder , parentObj . Instance , parentObj . ElementType , parentElement . NamespaceURI , parentElement . LocalName ) ;
299+ collectionProperty = FindExistingXamlProperty ( parentObj , propertyInfo ) ;
300+ collectionInstance = obj . Instance ;
301+ collectionType = obj . ElementType ;
302+ collectionPropertyElement = element ;
303+ } else if ( defaultProperty != null && defaultProperty . IsCollection && ! element . IsEmpty ) {
304+ foreach ( XmlNode childNode in elementChildNodes ) {
305+ XmlElement childElement = childNode as XmlElement ;
306+ if ( childElement == null || ! ObjectChildElementIsPropertyElement ( childElement ) ) {
307+ obj . AddProperty ( collectionProperty = new XamlProperty ( obj , defaultProperty ) ) ;
308+ collectionType = defaultProperty . ReturnType ;
309+ collectionInstance = defaultProperty . GetValue ( obj . Instance ) ;
310+ break ;
311+ }
312+ }
294313 }
295314
296- foreach ( XmlNode childNode in GetNormalizedChildNodes ( element ) ) {
315+ foreach ( XmlNode childNode in elementChildNodes ) {
297316 XmlElement childElement = childNode as XmlElement ;
298317 if ( childElement != null ) {
299318 if ( childElement . NamespaceURI == XamlConstants . XamlNamespace )
300319 continue ;
301320
302321 if ( ObjectChildElementIsPropertyElement ( childElement ) ) {
303- ParseObjectChildElementAsPropertyElement ( obj , childElement , defaultProperty , defaultPropertyValue ) ;
322+ ParseObjectChildElementAsPropertyElement ( obj , childElement , defaultProperty ) ;
304323 continue ;
305324 }
306325 }
307326 if ( initializeFromTextValueInsteadOfConstructor != null )
308327 continue ;
309328 XamlPropertyValue childValue = ParseValue ( childNode ) ;
310329 if ( childValue != null ) {
311- if ( defaultProperty != null && defaultProperty . IsCollection ) {
312- defaultCollectionProperty . ParserAddCollectionElement ( null , childValue ) ;
313- CollectionSupport . AddToCollection ( defaultProperty . ReturnType , defaultPropertyValue , childValue ) ;
330+ if ( collectionProperty != null ) {
331+ collectionProperty . ParserAddCollectionElement ( collectionPropertyElement , childValue ) ;
332+ CollectionSupport . AddToCollection ( collectionType , collectionInstance , childValue ) ;
314333 } else {
315334 if ( defaultProperty == null )
316335 throw new XamlLoadException ( "This element does not have a default value, cannot assign to it" ) ;
@@ -386,6 +405,16 @@ XamlPropertyValue ParseValueCore(XmlNode childNode)
386405 return null ;
387406 }
388407
408+ static XamlProperty FindExistingXamlProperty ( XamlObject obj , XamlPropertyInfo propertyInfo )
409+ {
410+ foreach ( XamlProperty existing in obj . Properties ) {
411+ if ( existing . propertyInfo . FullyQualifiedName == propertyInfo . FullyQualifiedName )
412+ return existing ;
413+ }
414+
415+ throw new XamlLoadException ( "Existing XamlProperty " + propertyInfo . FullyQualifiedName + " not found." ) ;
416+ }
417+
389418 static XamlPropertyInfo GetDefaultProperty ( Type elementType )
390419 {
391420 foreach ( ContentPropertyAttribute cpa in elementType . GetCustomAttributes ( typeof ( ContentPropertyAttribute ) , true ) ) {
@@ -533,7 +562,12 @@ static bool ObjectChildElementIsPropertyElement(XmlElement element)
533562 return element . LocalName . Contains ( "." ) ;
534563 }
535564
536- void ParseObjectChildElementAsPropertyElement ( XamlObject obj , XmlElement element , XamlPropertyInfo defaultProperty , object defaultPropertyValue )
565+ static bool IsElementChildACollectionForProperty ( XamlTypeFinder typeFinder , XmlElement element , XamlPropertyInfo propertyInfo )
566+ {
567+ return element . ChildNodes . Count == 1 && propertyInfo . ReturnType . IsAssignableFrom ( FindType ( typeFinder , element . FirstChild . NamespaceURI , element . FirstChild . LocalName ) ) ;
568+ }
569+
570+ void ParseObjectChildElementAsPropertyElement ( XamlObject obj , XmlElement element , XamlPropertyInfo defaultProperty )
537571 {
538572 Debug . Assert ( element . LocalName . Contains ( "." ) ) ;
539573 // this is a element property syntax
@@ -542,23 +576,29 @@ void ParseObjectChildElementAsPropertyElement(XamlObject obj, XmlElement element
542576 bool valueWasSet = false ;
543577
544578 object collectionInstance = null ;
579+ bool isElementChildACollectionForProperty = false ;
545580 XamlProperty collectionProperty = null ;
546581 if ( propertyInfo . IsCollection ) {
547582 if ( defaultProperty != null && defaultProperty . FullyQualifiedName == propertyInfo . FullyQualifiedName ) {
548- collectionInstance = defaultPropertyValue ;
549583 foreach ( XamlProperty existing in obj . Properties ) {
550584 if ( existing . propertyInfo == defaultProperty ) {
551585 collectionProperty = existing ;
552586 break ;
553587 }
554588 }
555- } else {
556- collectionInstance = propertyInfo . GetValue ( obj . Instance ) ;
557589 }
590+
558591 if ( collectionProperty == null ) {
559592 obj . AddProperty ( collectionProperty = new XamlProperty ( obj , propertyInfo ) ) ;
560593 }
561- collectionProperty . ParserSetPropertyElement ( element ) ;
594+
595+ isElementChildACollectionForProperty = IsElementChildACollectionForProperty ( settings . TypeFinder , element , propertyInfo ) ;
596+ if ( isElementChildACollectionForProperty )
597+ collectionProperty . ParserSetPropertyElement ( ( XmlElement ) element . FirstChild ) ;
598+ else {
599+ collectionInstance = collectionProperty . propertyInfo . GetValue ( obj . Instance ) ;
600+ collectionProperty . ParserSetPropertyElement ( element ) ;
601+ }
562602 }
563603
564604 XmlSpace oldXmlSpace = currentXmlSpace ;
@@ -570,7 +610,10 @@ void ParseObjectChildElementAsPropertyElement(XamlObject obj, XmlElement element
570610 XamlPropertyValue childValue = ParseValue ( childNode ) ;
571611 if ( childValue != null ) {
572612 if ( propertyInfo . IsCollection ) {
573- if ( collectionInstance != null ) {
613+ if ( isElementChildACollectionForProperty ) {
614+ collectionProperty . PropertyValue = childValue ;
615+ }
616+ else {
574617 CollectionSupport . AddToCollection ( propertyInfo . ReturnType , collectionInstance , childValue ) ;
575618 collectionProperty . ParserAddCollectionElement ( element , childValue ) ;
576619 }
0 commit comments