/// <summary> /// Adds an item to the underlying source, displaying in a specific position in rendered control. /// </summary> /// <param name="item"></param> /// <param name="nearItem"></param> /// <param name="addLocationHint"></param> public void AddToSource(object item, object nearItem, AddLocationHint addLocationHint) { CollectionTeaser collectionTeaser; if (CollectionTeaser.TryCreate(ItemsSource, out collectionTeaser)) { collectionTeaser.Add(item); } else { Items.Add(item); } MoveItem(new MoveItemRequest(item, nearItem, addLocationHint)); }
public static bool TryCreate(object items, out CollectionTeaser collectionTeaser) { collectionTeaser = null; var list = items as IList; if (list != null) { collectionTeaser = new CollectionTeaser(i => list.Add(i), list.Remove); } else if (items != null) { var itemsType = items.GetType(); var genericCollectionType = typeof(ICollection <>); //TODO, *IF* we really wanted to we could get the consumer to inform us of the correct type //if there are multiple impls. havent got time for this edge case right now though var collectionImplType = itemsType.GetInterfaces().SingleOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericCollectionType); if (collectionImplType != null) { var genericArgType = collectionImplType.GetGenericArguments().First(); var addMethodInfo = collectionImplType.GetMethod("Add", new[] { genericArgType }); var removeMethodInfo = collectionImplType.GetMethod("Remove", new[] { genericArgType }); collectionTeaser = new CollectionTeaser( i => addMethodInfo.Invoke(items, new[] { i }), i => removeMethodInfo.Invoke(items, new[] { i })); } } return(collectionTeaser != null); }