From 1bd00ea2202e672577336e49da65ba58756d4154 Mon Sep 17 00:00:00 2001
From: gumme <gumme@server.fake>
Date: Mon, 17 Jun 2013 21:25:52 +0200
Subject: [PATCH] Made it possible to add resources to a ResourceDictionary
 through DesignItemProperty.CollectionElements.

---
 .../Project/Xaml/XamlDesignItem.cs                |  5 +++++
 .../Project/CollectionElementsCollection.cs       |  4 +++-
 .../Project/CollectionSupport.cs                  | 15 +++++++++++++++
 .../WpfDesign/WpfDesign/Project/DesignItem.cs     |  5 +++++
 4 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignItem.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignItem.cs
index 83ae4df39f..60b1d308b7 100644
--- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignItem.cs
+++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignItem.cs
@@ -53,6 +53,11 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml
 			set { this.Properties["Name"].SetValue(value); }
 		}
 		
+		public override string Key {
+			get { return XamlObject.GetXamlAttribute("Key"); }
+			set { XamlObject.SetXamlAttribute("Key", value); }
+		}
+		
 		#if EventHandlerDebugging
 		static int totalEventHandlerCount;
 		#endif
diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionElementsCollection.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionElementsCollection.cs
index d37c2c82d1..f1ae0c21ba 100644
--- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionElementsCollection.cs
+++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionElementsCollection.cs
@@ -51,7 +51,9 @@ namespace ICSharpCode.WpfDesign.XamlDom
 		{
 			XamlPropertyInfo info = property.propertyInfo;
 			object collection = info.GetValue(property.ParentObject.Instance);
-			CollectionSupport.Insert(info.ReturnType, collection, item, index);
+			if (!CollectionSupport.TryInsert(info.ReturnType, collection, item, index)) {
+				CollectionSupport.AddToCollection(info.ReturnType, collection, item);
+			}
 			
 			item.ParentProperty = property;
 			property.InsertNodeInCollection(item.GetNodeForCollection(), index);
diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs
index f41ca0f023..5907fce5bd 100644
--- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs
+++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs
@@ -94,6 +94,21 @@ namespace ICSharpCode.WpfDesign.XamlDom
 				CultureInfo.InvariantCulture);
 		}
 		
+		/// <summary>
+		/// Adds a value at the specified index in the collection. A return value indicates whether the Insert succeeded.
+		/// </summary>
+		/// <returns>True if the Insert succeeded, false if the collection type does not support Insert.</returns>
+		internal static bool TryInsert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index)
+		{
+			try {
+				Insert(collectionType, collectionInstance, newElement, index);
+			} catch (MissingMethodException) {
+				return false;
+			}
+			
+			return true;
+		}
+		
 		static readonly Type[] RemoveAtParameters = { typeof(int) };
 		
 		/// <summary>
diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/DesignItem.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/DesignItem.cs
index ee93c82920..7ab86de912 100644
--- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/DesignItem.cs
+++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign/Project/DesignItem.cs
@@ -64,6 +64,11 @@ namespace ICSharpCode.WpfDesign
 		/// </summary>
 		public abstract string Name { get; set; }
 		
+		/// <summary>
+		/// Gets/Sets the value of the "x:Key" attribute on the design item.
+		/// </summary>
+		public abstract string Key { get; set; }
+		
 		/// <summary>
 		/// Is raised when the name of the design item changes.
 		/// </summary>