I have a WPF ListView that I am trying to filter within a BackgroundWorker. My code is shown below:
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
This code runs through the filtering however it fails with an error "The calling thread cannot access this object because a different thread owns it." on the following line:
ListView.Filter = New Predicate(Of Object)(AddressOf Me.FindItemsAsync)
What would be the problem here? I can't seem to find any samples with filtering through the BackgroundWorker.
Update: Does anyone know of a sample that filters a WPF ListView using a BackgroundWorker?