开发者

How to change reference to ItemsSource Ilist collection modify to ItemsSource collection generated from XMLDataProvider?

开发者 https://www.devze.com 2023-01-27 13:41 出处:网络
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. Th

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); 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消