我在执行ListCollectionView.AddNewItem添加对象后,IndexOf返回的索引为-1,不知道是什么原因?
代码如下:
List PortNoindexData = new List()
{
new ComboBoxItem() { Content = 1 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
new ComboBoxItem() { Content = 2 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
new ComboBoxItem() { Content = 3 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
new ComboBoxItem() { Content = 4 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
new ComboBoxItem() { Content = 5 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
new ComboBoxItem() { Content = 6 , VerticalAlignment = VerticalAlignment. Center,FontSize = 14 },
};
ComboBox_PortNo.ItemsSource = PortNoindexData;
ListCollectionView viewPort = (ListCollectionView)CollectionViewSource.GetDefaultView(ComboBox_PortNo.ItemsSource);
//viewPort已经删除了PortNoindexData[2]
viewPort.AddNewItem(PortNoindexData[2]);
viewPort.CommitNew();
viewPort.IndexOf(PortNoindexData[2]);