我知道我之前发布的类似问题,但我已经尝试了各种建议,似乎没有任何效果。
以下是我的问题:尝试从列表框控件中删除项目时出现以下错误:
附加信息:在使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource访问和修改元素。
我需要帮助从WPF中使用VB.Net代码隐藏中的列表框中删除项目。 当我单击列表框中的一行时,我想从列表框中删除该项。 我创建对象'ToLoadImages'的列表以加载到我的列表框中。
如何从列表框和列表ob对象'ToLoadImages'中删除所选项目?
请参阅下面的代码:
'代码加载列表框
Private Sub GetListToLoad(ClaimNo As String)
Dim ta As New ImagesDataSetTableAdapters.usp_SELECT_ImageTableAdapter
Dim dt As ImagesDataSet.usp_SELECT_ImageDataTable = ta.GetData(1, _ClaimNo, True)
Dim dr As DataRow
ListToLoad = New List(Of ToLoadImages)
Dim i As Integer = 0
For Each dr In dt
Dim ImgSource2() As Byte = DirectCast(dr(7), Byte())
Dim stream2 As MemoryStream = New MemoryStream
stream2.Write(ImgSource2, 0, ImgSource2.Length - 1)
stream2.Seek(0, SeekOrigin.Begin)
bitMap2 = New BitmapImage
bitMap2.BeginInit()
bitMap2.StreamSource = stream2
bitMap2.EndInit()
ListToLoad.Add(New ToLoadImages(dr(0), bitMap2))
Next
ImageListBox.ItemsSource = ListToLoad
End Sub
'用于从列表框中检索所选项目的代码 “我想在这里添加将从列表框和”ToLoadImages“列表中删除所选项目的代码。
Private Sub ImageListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ImageListBox.SelectionChanged
Try
Dim itemsToLoad As ToLoadImages
itemsToLoad = ImageListBox.SelectedItem
Dim imageID as String = itemsToLoad.ImgID.ToString
Catch ex As Exception
MsgBox("Error encountered.")
End Try
End Sub
Class ToLoadImages
Public Class ToLoadImages
Private m_imgID As Integer
Private m_imageX As BitmapImage
Public Sub New(imgID As Integer, imagex As BitmapImage)
Me.m_imgID = imgID
Me.m_imageX = imagex
End Sub
Public Property ImgID() As Integer
Get
Return m_imgID
End Get
Set(ByVal value As Integer)
m_imgID = value
End Set
End Property
Public Property ImageX() As BitmapImage
Get
Return m_imageX
End Get
Set(ByVal value As BitmapImage)
m_imageX = value
End Set
End Property
End Class
答案 0 :(得分:0)
使用ObservableCollection
代替List
。
ObservableCollection
自动更新更改