I am trying to modify a code where the content generated with ObservableCollection to collection generated with XMLDataprovider. I am able to generate the content with XMLDataProvider successfully. The problem I am trying to solve now is to modify the code where it is referenced to the content generated from ObservableCollection. Any time I run the method below, my application is getting frozen. It looks like IList is not appropriate reference to XML collection. What should be used instead? Thank you in advance.
public static void InsertItemInItemsControl(ItemsControl itemsControl, object itemToInsert, int insertionIndex)
{
if (itemToI开发者_StackOverflow社区nsert != null)
{
IEnumerable itemsSource = itemsControl.ItemsSource;
if (itemsSource == null)
{
itemsControl.Items.Insert(insertionIndex, itemToInsert);
}
// It looks like IList is not appropriate reference to XML collection else if (itemsSource is IList)
{
((IList)itemsSource).Insert(insertionIndex, itemToInsert);
}
else
{
Type type = itemsSource.GetType();
Type genericIListType = type.GetInterface("IList`1");
if (genericIListType != null)
{
type.GetMethod("Insert").Invoke(itemsSource, new object[] { insertionIndex, itemToInsert });
}
}
}
}
Why are you doing this?:
// it looks like IList is not appropriate reference to XML collection
else if (itemsSource is IList)
{
((IList)itemsSource).Insert(insertionIndex, itemToInsert);
}
精彩评论