TakeWhile taking all elements despite selection criterion

RogerSchlueter-7899 1,281 Reputation points
2020-06-12T05:28:58.787+00:00

I am trying to create a progressive ComboBox. For this example I am using a Person Class with properties ID and Name.

The following code almost implements the desired functionality:

Dim ocPeople As New ObservableCollection(Of Person)
Dim cvsPeople As CollectionViewSource
Dim tb As TextBox
Dim pup As Popup

Private Sub Me_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        GetData()
        tb = DirectCast(cbo.Template.FindName("PART_EditableTextBox", cbo), TextBox)
        AddHandler tb.TextChanged, AddressOf Search
        cvsPeople = DirectCast(Resources("cvsPeople"), CollectionViewSource)
        cvsPeople.Source = ocPeople
        cbo.SelectedIndex = -1
        pup = DirectCast(cbo.Template.FindName("PART_Popup", cbo), Popup)
    End Sub

    Public Sub Search(sender As Object, e As TextChangedEventArgs)
        cbo.ItemsSource.Cast(Of Person).TakeWhile(Function(p) p.Name.StartsWith(tb.Text))
        If pup IsNot Nothing Then pup.IsOpen = True
    End Sub

The problem is that TakeWhile is not working. It returns all the items in the ComboBox, not just the ones matching the stated criterion.

Ignore the Tag as they are implemented yet.

Azure Service Fabric
Azure Service Fabric
An Azure service that is used to develop microservices and orchestrate containers on Windows and Linux.
262 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.