У меня есть простой проект MVVM с данными времени проектирования, которые хорошо работают и с главными данными и со списком с контролем за работой пользователей для каждого из детских пунктов.
Однако, то, когда я иллюстрирую примерами классы во время выполнения, основные данные показывают, однако, детские данные, не показывает (но у списка есть правильное количество пунктов, однако у них нет ни одного показа данных в textboxes).
Я заметил, что конструктора на Спорте называют далеким к много раз, по сравнению с тем, что я ожидал бы (например, во время выполнения, я буду ожидать, что он будет назван только дважды, но это, кажется, называют больше, чем это).
I have class Person, and Sport. Every person can like multiple sports and have a favorite team.
I have a personViewModel & sportViewModel which inherit from viewModelBase.
Вот мой Кодекс VB.net
Imports System.Collections.ObjectModel
Public Class Person
Public Property Forename As String
Public Property Surname As String
Public Sports As New ObservableCollection(Of Sport)
End Class
Public Class Sport
Public Sub New()
Debug.WriteLine("test")
End Sub
Public Property SportName As String
Public Property FavouriteProfessionalTeam As String
End Class
Imports System.Collections.ObjectModel
Namespace ViewModel
Public Class PersonViewModel
Inherits ViewModel.ViewModelBase
Public Property Person1 As Person
Public Property SportViewModels As New ObservableCollection(Of SportViewModel)
Public Sub New()
LoadData()
End Sub
'''
''' Loads the data for the application.
'''
Private Sub LoadData()
If IsInDesignModeStatic Then
LoadDesignData()
Else
End If
End Sub
'''
''' Loads temporary data for use in the designer.
'''
Private Sub LoadDesignData()
Person1 = New Person
Person1.Forename = "Mickey Run Time"
Person1.Surname = "Mouse Run Time"
Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Man Utd", .SportName = "Soccer"})
Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Barcelona", .SportName = "Spanish Soccer"})
Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Ulster", .SportName = "Rugby"})
For Each sport1 In Person1.Sports
Dim sportVm As New SportViewModel With {.Sport1 = sport1}
SportViewModels.Add(sportVm)
Next
End Sub
End Class
End Namespace
Namespace ViewModel
Public Class SportViewModel
Inherits ViewModel.ViewModelBase
Public Property Sport1 As New Sport
Public Property Person1 As Person
Public Sub New()
LoadData()
End Sub
'''
''' Loads the data for the application.
'''
Private Sub LoadData()
If IsInDesignModeStatic Then
LoadDesignData()
Else
' Debug.WriteLine(Sport1.SportName)
' Load the student data asynchronously
'StudentContextInstance = New StudentContext
'Dim loadop =
' StudentContextInstance.Load(StudentContextInstance.
' GetStudentsQuery(),
' AddressOf OnStudentsLoaded, Nothing)
End If
End Sub
'''
''' Loads temporary data for use in the designer.
'''
Private Sub LoadDesignData()
Sport1 = New Sport With {.SportName = "Design Time Name", .FavouriteProfessionalTeam = "Design Time Team"}
End Sub
End Class
End Namespace
Imports System.ComponentModel
Namespace ViewModel
Public Class ViewModelBase
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Shared _isInDesignMode As Boolean?
Public Shared ReadOnly Property IsInDesignModeStatic As Boolean
Get
If Not _IsInDesignMode.HasValue Then
_IsInDesignMode = DesignerProperties.GetIsInDesignMode(New DependencyObject)
End If
Return _IsInDesignMode.Value
End Get
End Property
Protected Sub OnPropertyChanged(ByVal propertyName As String)
' Send an event notification that the property changed
' This allows the UI to know when one of the items changes
If Not String.IsNullOrEmpty(propertyName) Then
RaiseEvent PropertyChanged(Me,
New PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
End Namespace
Вот код позади для моего окна повозки, которое настраивает MVVM для показа
Система импорта. Коллекции. ObjectModel
Импорт WpfApplicationMVVMTest. ViewModel
Class MainWindow
Public Property Person1 As Person
Public Property SportViewModels As New ObservableCollection(Of SportViewModel)
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim wndPerson As New PersonWindow
Person1 = New Person
Person1.Forename = "Donald"
Person1.Surname = "Duck"
Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Man Utd", .SportName = "Soccer"})
Person1.Sports.Add(New Sport With {.FavouriteProfessionalTeam = "Barcelona", .SportName = "Spanish Soccer"})
For Each sport1 In Person1.Sports
Dim sportVm As New SportViewModel With {.Sport1 = sport1}
SportViewModels.Add(sportVm)
Next
Dim vm As New ViewModel.PersonViewModel
vm.SportViewModels = SportViewModels
vm.Person1 = Person1
wndPerson.DataContext = vm
wndPerson.Show()
End Sub
End Class
Вот Кодекс XAML
Here is my design time image with 3 Sports displayed (as per my load design time data).

Here is my run time window with 2 sports displaying (although they have no data - which is the issue).
