为可观察的集合实现中介设计模式

时间:2014-12-18 06:29:50

标签: c# wpf vb.net mvvm observablecollection

我有一个应用程序,我想在我的MVVM应用程序中实现Mediator Design Pattern

问题在于它适用于属性,但它不适用于集合。我解释说我有这门课:

Public Class Eleve

    Private _Nom As String
    Private _Prenom As String
    Private _Age As Integer

    Public Property Nom As String
        Get
            Return _Nom
        End Get
        Set(ByVal value As String)
            _Nom = value
        End Set
    End Property

    Public Property Prenom As String
        Get
            Return _Prenom
        End Get
        Set(ByVal value As String)
            _Prenom = value
        End Set
    End Property

    Public Property Age As Integer
        Get
            Return _Age
        End Get
        Set(ByVal value As Integer)
            _Age = value
        End Set
    End Property
End Class

在第一个视图模型中,我有这个集合:

 Public Property LstEtudiants As ObservableCollection(Of Eleve)
            Get
                Return _LstEtudiants
            End Get
            Set(ByVal value As ObservableCollection(Of Eleve))
                _LstEtudiants = value
                RaiseEvent CollectionChanged(Me, New NotifyCollectionChangedEventArgs("LstEtudiants"))
                RaiseEvent UpdateGrids(value)
            End Set
        End Property

事件UpdateGrids在中介类中处理:

Public Class Form1ViewModel


#Region "Classes collègues"
    Public objInfoViewModel As InfoViewModel
    Public objResultViewModel As ResultViewModel
#End Region

#Region "Constructor"
    Public Sub New()
        objInfoViewModel = New InfoViewModel
        objResultViewModel = New ResultViewModel
        AddHandler objInfoViewModel.ChangeStatut, AddressOf OnChangingStatut
        AddHandler objInfoViewModel.UpdateGrids, AddressOf OnUpdateGrids

    End Sub
#End Region


#Region "Methods"

        Private Sub OnChangingStatut(ByVal intAge As Integer)
            If (objResultViewModel IsNot Nothing) Then
                If (intAge > 0 AndAlso intAge < 10) Then objResultViewModel.Result = "Enfant"
                If (intAge >= 10 AndAlso intAge < 30) Then objResultViewModel.Result = "Adolescent"
                If (intAge >= 30 AndAlso intAge < 50) Then objResultViewModel.Result = "Homme"
                If (intAge >= 50) Then objResultViewModel.Result = "Vieux"
            End If
        End Sub

        Private Sub OnUpdateGrids(ByVal lstEleve As ObjectModel.ObservableCollection(Of Eleve))
            If (objResultViewModel IsNot Nothing) Then
                objResultViewModel.LstEnfant = New ObservableCollection(Of Eleve)(lstEleve.ToList.Where(Function(x) x.Age > 0 And x.Age < 10))
                objResultViewModel.LstAdolescent = New ObservableCollection(Of Eleve)(lstEleve.ToList.Where(Function(x) x.Age >= 10 And x.Age < 30))
                objResultViewModel.LstHomme = New ObservableCollection(Of Eleve)(lstEleve.ToList.Where(Function(x) x.Age >= 30 And x.Age < 50))
                objResultViewModel.LstVieux = New ObservableCollection(Of Eleve)(lstEleve.ToList.Where(Function(x) x.Age > 51))
            End If
        End Sub
    #End Region



    End Class

问题是不会引发或处理修改网格(UpdateGrids)的事件。所以:

  1. 我提交的错误是什么?
  2. 我该如何解决?

1 个答案:

答案 0 :(得分:1)

在_LstEtudiants声明前添加“With Events”。然后处理集合更改事件。 列表 setter 通常不是您在MVVM应用程序中关注的位置。通常,列表中项目的操作就是您的目标。处理集合更改事件是您可以执行此操作的地方。