-1

I've got an ObservableCollection(Of PdfMarkupAnnotationDataWrapper) which is bound to a ListBoxEdit. In addition I've got a textbox which should work as a filter.

Now when the user types something in the textbox the ObservableCollection in my Viewmodel should be filtered by the input of the textbox.

Here is my collection

Private Property _annotationList As ObservableCollection(Of PdfMarkupAnnotationDataWrapper)

Public Property AnnotationList As ObservableCollection(Of 

PdfMarkupAnnotationDataWrapper)
      Get
         Return _annotationList
      End Get
      Set(value As ObservableCollection(Of PdfMarkupAnnotationDataWrapper))
         _annotationList = value
         OnPropertyChanged()
      End Set
   End Property

Is there any way to accomplish this task?

I was thinking about copying the collection but there has to be better solution.

Detlef D Soost
  • 83
  • 3
  • 11

1 Answers1

0

I was thinking about copying the collection but there has to be better solution.

That would be to remove or add items from it. But you still need to store the original unfiltered items in another collection.

You could use LINQ to do the filtering, e.g.:

Private _unFilteredAnnotationList As ObservableCollection(Of PdfMarkupAnnotationDataWrapper) 'make sure to populate this one
Private _annotationList As ObservableCollection(Of PdfMarkupAnnotationDataWrapper)
Public Property AnnotationList As ObservableCollection(Of PdfMarkupAnnotationDataWrapper)
    Get
        Return _annotationList
    End Get
    Set(value As ObservableCollection(Of PdfMarkupAnnotationDataWrapper))
        _annotationList = value
        OnPropertyChanged()
    End Set
End Property

Private _text As String
Public Property Text As String
    Get
        Return _text
    End Get
    Set(value As String)
        _text = value
        OnPropertyChanged()
        'filter
        AnnotationList = New ObservableCollection(Of PdfMarkupAnnotationDataWrapper)(_unFilteredAnnotationList.Where(Function(x)
                                                                                                                         Return x.YourStringProperty.StartsWith(_text)
                                                                                                                     End Function))
    End Set
End Property
mm8
  • 163,881
  • 10
  • 57
  • 88