Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM binding with design time data works correctly at design time, but not at run time
    primarykey
    data
    text
    <p>I have a simple MVVM project with design time data which works fine with both the main data and a listbox with user controls for each of the child items. However when I instantiate the classes at run time, the the master data displays however the child data does not display (but the listbox has the correct number of items, however they don't have any of the data displaying in the textboxes ).</p> <p>I have noticed the constructor on Sport gets called far to many times, compared to what I'd expect (e.g. at run time, I'd expect it to be called only twice, but it seems to be called more than that).</p> <p>I have class Person, and Sport. Every person can like multiple sports and have a favorite team. I have a personViewModel &amp; sportViewModel which inherit from viewModelBase.</p> <p>Here is my VB.net Code</p> <pre><code>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 ''' &lt;summary&gt; ''' Loads the data for the application. ''' &lt;/summary&gt; Private Sub LoadData() If IsInDesignModeStatic Then LoadDesignData() Else End If End Sub ''' &lt;summary&gt; ''' Loads temporary data for use in the designer. ''' &lt;/summary&gt; 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 ''' &lt;summary&gt; ''' Loads the data for the application. ''' &lt;/summary&gt; 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 ''' &lt;summary&gt; ''' Loads temporary data for use in the designer. ''' &lt;/summary&gt; 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 </code></pre> <p>Here is the code behind for my wain window which sets up the MVVM for display Imports System.Collections.ObjectModel Imports WpfApplicationMVVMTest.ViewModel</p> <pre><code>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 </code></pre> <p>Here is the XAML Code</p> <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ViewModel="clr-namespace:WpfApplicationMVVMTest.ViewModel" xmlns:View="clr-namespace:WpfApplicationMVVMTest" mc:Ignorable="d" x:Class="PersonWindow" Title="PersonWindow" Height="1114.8" Width="542"&gt; &lt;Window.Resources&gt;&lt;/Window.Resources&gt; &lt;Window.DataContext&gt; &lt;ViewModel:PersonViewModel /&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;Grid x:Name="Grid1" HorizontalAlignment="Left" Margin="41,39,0,0" VerticalAlignment="Top"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Label Content="Forename:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/&gt; &lt;TextBox x:Name="ForenameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding Person1.Forename, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/&gt; &lt;Label Content="Surname:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/&gt; &lt;TextBox x:Name="SurnameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding Person1.Surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/&gt; &lt;/Grid&gt; &lt;ListBox Margin="41,108,59,753" ItemsSource="{Binding Path=SportViewModels}" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;View:SportUserControl DataContext="{Binding}" Margin="5"/&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Window&gt; &lt;UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ViewModel="clr-namespace:WpfApplicationMVVMTest.ViewModel" x:Class="SportUserControl" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="600"&gt; &lt;UserControl.Resources&gt;&lt;/UserControl.Resources&gt; &lt;UserControl.DataContext&gt; &lt;ViewModel:SportViewModel /&gt; &lt;/UserControl.DataContext&gt; &lt;Grid&gt; &lt;Grid x:Name="Grid1" HorizontalAlignment="Left" Margin="31,26,0,0" VerticalAlignment="Top"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Label Content="Favourite Professional Team:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/&gt; &lt;TextBox x:Name="FavouriteProfessionalTeamTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding Sport1.FavouriteProfessionalTeam, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/&gt; &lt;Label Content="Sport Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/&gt; &lt;TextBox x:Name="SportNameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding Sport1.SportName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/&gt; &lt;/Grid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Here is my design time image with 3 Sports displayed (as per my load design time data). <img src="https://i.stack.imgur.com/gn2Ud.png" alt="enter image description here"></p> <p>Here is my run time window with 2 sports displaying (although they have no data - which is the issue). <img src="https://i.stack.imgur.com/wjZgr.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload