在过滤观察集合后检查行时出错

时间:2016-06-23 03:51:24

标签: wpf vb.net

我在编辑可观察的集合时遇到问题...我在代码隐藏中绑定了这样的列......

Dim dgCheckBoxColumn As New DataGridCheckBoxColumn()
Dim column_username As New DataGridTextColumn()
Dim textColumn2 As New DataGridTextColumn()
Dim textColumn3 As New DataGridTextColumn()

dgCheckBoxColumn.Header = "Selected"
dgCheckBoxColumn.Binding = New Binding("Selected")
dgvResults.Columns.Add(dgCheckBoxColumn)

column_username.Header = "User Name"
column_username.Binding = New Binding("accountName")
dgvResults.Columns.Add(column_username)

textColumn2.Header = "First Name"
textColumn2.Binding = New System.Windows.Data.Binding("firstName")
dgvResults.Columns.Add(textColumn2)

textColumn3.Header = "Last Name"
textColumn3.Binding = New System.Windows.Data.Binding("lastName")
dgvResults.Columns.Add(textColumn3)

然后我创建了我的observablecollection ......

Dim oc_userlist As New ObservableCollection(Of user)

Imports System.ComponentModel

Public Class user

    Implements INotifyPropertyChanged

    Private m_accountname As String
    Private m_firstname As String
    Private m_lastname As String
    Private _Selected As Boolean

    Public Property Selected() As Boolean
        Get
            Return _Selected
        End Get
        Set(value As Boolean)
            _Selected = value
            NotifyPropertyChanged("IsChecked")
        End Set
    End Property

    Public Property accountName() As String
        Get
            Return m_accountname
        End Get
        Set(value As String)
            m_accountname = value
        End Set
    End Property

    Public Property firstName() As String
        Get
            Return m_firstname
        End Get
        Set(value As String)
            m_firstname = value
        End Set
    End Property

    Public Property lastName() As String
        Get
            Return m_lastname
        End Get
        Set(value As String)
            m_lastname = value
        End Set
    End Property

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

#End Region

#Region "Private Helpers"

    Private Sub NotifyPropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

#End Region

End Class

然后我将新用户添加到集合中,就像这样......

oc_userlist.Add(New user With { _
.Selected = False, _
.accountName = "awiles", _
.firstName = "Anthony", _
.lastName = "Wiles"
})

我正在尝试使用带有textchanged事件的文本框进行过滤,并且它运行得非常好......

Private Sub TextBox_TextChanged(sender As Object, e As TextChangedEventArgs)


        Dim result = oc_userlist.Where(Function(w) w.accountName.Contains(txtFilter.Text.ToString) _
                                           Or w.firstName.Contains(txtFilter.Text.ToString) _
                                           Or w.lastName.Contains(txtFilter.Text.ToString))
        dgvResults.ItemsSource = result

End Sub

我遇到的问题是,一旦使用文本框过滤,然后我尝试选择一些东西,它告诉我

'EditItem' is not allowed for this view.

我在试图解决这个问题时遇到了问题,有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

原因可能是您的Linq查询(使用Where)不是List(Of T)而是IEnumerable。

尝试添加对ToList()的调用

Dim result = oc_userlist.Where(Function(w) w.accountName.Contains(txtFilter.Text.ToString) _
   Or w.firstName.Contains(txtFilter.Text.ToString) _
   Or w.lastName.Contains(txtFilter.Text.ToString)) _
   .ToList()

然后告诉它是否有效