【问题标题】:Filter observablecollection (mvvm) [duplicate]过滤 observablecollection (mvvm) [重复]
【发布时间】:2018-06-01 09:56:28
【问题描述】:

我有一个绑定到ListBoxEditObservableCollection(Of PdfMarkupAnnotationDataWrapper)。此外,我还有一个textbox,它应该可以用作过滤器。

现在,当用户在 textbox 中键入内容时,我的 Viewmodel 中的 ObservableCollection 应该被 textbox 的输入过滤。

这是我的收藏

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

有没有办法完成这个任务?

我正在考虑复制集合,但必须有更好的解决方案。

【问题讨论】:

    标签: wpf vb.net mvvm filter observable


    【解决方案1】:

    我正在考虑复制集合,但必须有更好的解决方案。

    这将是从中删除或添加项目。但是您仍然需要将原始未过滤的项目存储在另一个集合中。

    您可以使用 LINQ 进行过滤,例如:

    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
    

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 2016-03-06
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多