【问题标题】:VB.NET Index '0' is out of range?VB.NET 索引“0”超出范围?
【发布时间】:2013-06-01 18:53:38
【问题描述】:

我在 WPF 中使用 ItemSource 以及所有使 UI 刷新它的类和事件创建了一个 ListBox。但是我的Remove方法有问题:

Public Sub Remove(ItemIndex As Integer)
MyList.RemoveAt(ItemIndex)
RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, MyList(ItemIndex)))
End Sub

但是当我执行此操作时,我收到一条消息,指出索引(在本例中为 ItemIndex)超出范围。但在输出窗口中它显示索引为“0”(否则它将从MyList 中删除该项目)。

【问题讨论】:

  • 好吧,如果列表中没有项目,索引0(索引第一个项目)将超出范围。
  • 为什么需要以编程方式引发事件?

标签: vb.net indexing range out inotifycollectionchanged


【解决方案1】:

问题解决了!我改了代码

Public Sub Remove(ItemIndex As Integer)
MyList.RemoveAt(ItemIndex)
RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, MyList(ItemIndex)))
End Sub

进入

Public Sub Remove(ItemIndex As Integer)
RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, MyList(ItemIndex), ItemIndex))
MyList.RemoveAt(ItemIndex)
End Sub

就是这样。

【讨论】:

    猜你喜欢
    • 2016-12-16
    • 2012-04-07
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多