我有一个 WPF ListView,我试图在 BackgroundWorker 中进行过滤。我的代码如下所示:
Dim Worker As New BackgroundWorker
AddHandler Worker.DoWork, AddressOf Me.FilterAsync
Me.TextBoxText = Me.TextBox.Text
Worker.RunWorkerAsync(Me.TextBox)
Private Sub FilterAsync(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'
Dim BackgroundWorker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim Text As String = e.Argument.ToString
'
Dim ListView As ListCollectionView = CType(CollectionViewSource.GetDefaultView(Me.ListView.ItemsSource), ListCollectionView)
If Text <> String.Empty Then
ListView.Filter = New Predicate(Of Object)(AddressOf Me.FindItemsAsync)
Else
ListView.Filter = Nothing
End If
'
End Sub
此代码通过过滤运行,但失败并显示错误“调用线程无法访问此对象,因为不同的线程拥有它。” 在以下行:
ListView.Filter = New Predicate(Of Object)(AddressOf Me.FindItemsAsync)
这里会有什么问题?我似乎找不到任何通过 BackgroundWorker 过滤的样本。
更新:有谁知道使用 BackgroundWorker 过滤 WPF ListView 的示例?