|
|
@ -21,6 +21,7 @@ using System.Diagnostics; |
|
|
|
using System.Collections; |
|
|
|
using System.Collections; |
|
|
|
using System.ComponentModel; |
|
|
|
using System.ComponentModel; |
|
|
|
using System.Globalization; |
|
|
|
using System.Globalization; |
|
|
|
|
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Reflection; |
|
|
|
using System.Windows; |
|
|
|
using System.Windows; |
|
|
|
using System.Windows.Documents; |
|
|
|
using System.Windows.Documents; |
|
|
@ -39,11 +40,11 @@ namespace ICSharpCode.WpfDesign.XamlDom |
|
|
|
public static bool IsCollectionType(Type type) |
|
|
|
public static bool IsCollectionType(Type type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return type != typeof(LineBreak) && ( |
|
|
|
return type != typeof(LineBreak) && ( |
|
|
|
typeof(IList).IsAssignableFrom(type) |
|
|
|
typeof(IList).IsAssignableFrom(type) |
|
|
|
|| type.IsArray |
|
|
|
|| type.IsArray |
|
|
|
|| typeof(IAddChild).IsAssignableFrom(type) |
|
|
|
|| typeof(IAddChild).IsAssignableFrom(type) |
|
|
|
|| typeof(IDictionary).IsAssignableFrom(type)); |
|
|
|
|| typeof(IDictionary).IsAssignableFrom(type)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets if the collection type <paramref name="col"/> can accepts items of type
|
|
|
|
/// Gets if the collection type <paramref name="col"/> can accepts items of type
|
|
|
@ -85,12 +86,12 @@ namespace ICSharpCode.WpfDesign.XamlDom |
|
|
|
} else if (collectionInstance is IDictionary) { |
|
|
|
} else if (collectionInstance is IDictionary) { |
|
|
|
object val = newElement.GetValueFor(null); |
|
|
|
object val = newElement.GetValueFor(null); |
|
|
|
object key = newElement is XamlObject ? ((XamlObject)newElement).GetXamlAttribute("Key") : null; |
|
|
|
object key = newElement is XamlObject ? ((XamlObject)newElement).GetXamlAttribute("Key") : null; |
|
|
|
if (key == null || key == "") |
|
|
|
if (key == null || key == "") |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (val is Style) |
|
|
|
if (val is Style) |
|
|
|
key = ((Style)val).TargetType; |
|
|
|
key = ((Style)val).TargetType; |
|
|
|
} |
|
|
|
} |
|
|
|
if (key == null || (key as string) == "") |
|
|
|
if (key == null || (key as string) == "") |
|
|
|
key = val; |
|
|
|
key = val; |
|
|
|
((IDictionary)collectionInstance).Add(key, val); |
|
|
|
((IDictionary)collectionInstance).Add(key, val); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -102,16 +103,24 @@ namespace ICSharpCode.WpfDesign.XamlDom |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a value at the specified index in the collection.
|
|
|
|
/// Adds a value at the specified index in the collection.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public static void Insert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index) |
|
|
|
public static bool Insert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
collectionType.InvokeMember( |
|
|
|
var hasInsert = collectionType.GetMethods().Any(x => x.Name == "Insert"); |
|
|
|
"Insert", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, |
|
|
|
|
|
|
|
null, collectionInstance, |
|
|
|
if (hasInsert) { |
|
|
|
new object[] { index, newElement.GetValueFor(null) }, |
|
|
|
collectionType.InvokeMember( |
|
|
|
CultureInfo.InvariantCulture); |
|
|
|
"Insert", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, |
|
|
|
|
|
|
|
null, collectionInstance, |
|
|
|
|
|
|
|
new object[] { index, newElement.GetValueFor(null) }, |
|
|
|
|
|
|
|
CultureInfo.InvariantCulture); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
@ -121,7 +130,7 @@ namespace ICSharpCode.WpfDesign.XamlDom |
|
|
|
internal static bool TryInsert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index) |
|
|
|
internal static bool TryInsert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index) |
|
|
|
{ |
|
|
|
{ |
|
|
|
try { |
|
|
|
try { |
|
|
|
Insert(collectionType, collectionInstance, newElement, index); |
|
|
|
return Insert(collectionType, collectionInstance, newElement, index); |
|
|
|
} catch (MissingMethodException) { |
|
|
|
} catch (MissingMethodException) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|