【问题标题】:How to change reference to ItemsSource Ilist collection modify to ItemsSource collection generated from XMLDataProvider?如何更改对 ItemsSource Ilist 集合的引用修改为从 XMLDataProvider 生成的 ItemsSource 集合?
【发布时间】: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 });
                }
            }
        }
    }

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    你为什么要这样做?:

    // it looks like IList is not appropriate reference to XML collection
    else if (itemsSource is IList) 
    { 
        ((IList)itemsSource).Insert(insertionIndex, itemToInsert); 
    } 
    

    【讨论】:

    • 我使用 ObservableCollection 的示例,我需要使用 XMLDataProvider 生成内容。上面的方法是来自此处的拖放示例的代码部分:bea.stollnitz.com/blog/?p=53。如果我回答了您的问题,请告诉我。
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2012-02-21
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多