【发布时间】:2012-08-18 06:03:23
【问题描述】:
我正在尝试将使用 ObservableCollection 生成的内容修改为使用 XMLDataprovider 生成的集合的代码。我能够成功地使用 XMLDataProvider 生成内容。我现在要解决的问题是修改引用从 ObservableCollection 生成的内容的代码。每当我运行下面的方法时,我的应用程序都会被冻结。看起来 IList 不适合引用 XML 集合。应该改用什么?提前谢谢你。
public static void InsertItemInItemsControl(ItemsControl itemsControl, object itemToInsert, int insertionIndex)
{
if (itemToInsert != 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 });
}
}
}
}
【问题讨论】: