Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9099951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:44:48+00:00 2026-06-17T00:44:48+00:00

I have a simple MVVM project with design time data which works fine with

  • 0

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 ).

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).

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.

Here is my VB.net 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

        ''' <summary> 
        ''' Loads the data for the application. 
        ''' </summary> 
        Private Sub LoadData()
            If IsInDesignModeStatic Then
                LoadDesignData()
            Else

            End If
        End Sub

        ''' <summary> 
        ''' Loads temporary data for use in the designer. 
        ''' </summary> 
        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

        ''' <summary> 
        ''' Loads the data for the application. 
        ''' </summary> 
        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

        ''' <summary> 
        ''' Loads temporary data for use in the designer. 
        ''' </summary> 
        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

Here is the code behind for my wain window which sets up the MVVM for display
Imports System.Collections.ObjectModel
Imports 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

Here is the XAML Code

<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">
    <Window.Resources></Window.Resources>
    <Window.DataContext>
        <ViewModel:PersonViewModel />
    </Window.DataContext>

    <Grid>
        <Grid x:Name="Grid1" HorizontalAlignment="Left" Margin="41,39,0,0" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Forename:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
            <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"/>
            <Label Content="Surname:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
            <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"/>
        </Grid>
        <ListBox Margin="41,108,59,753" ItemsSource="{Binding Path=SportViewModels}"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <View:SportUserControl DataContext="{Binding}"  Margin="5"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

<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">
    <UserControl.Resources></UserControl.Resources>
    <UserControl.DataContext>
        <ViewModel:SportViewModel />
    </UserControl.DataContext>


    <Grid>
        <Grid x:Name="Grid1" HorizontalAlignment="Left" Margin="31,26,0,0" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Favourite Professional Team:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
            <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"/>
            <Label Content="Sport Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
            <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"/>
        </Grid>

    </Grid>
</UserControl>

Here is my design time image with 3 Sports displayed (as per my load design time data).
enter image description here

Here is my run time window with 2 sports displaying (although they have no data – which is the issue).
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:44:50+00:00Added an answer on June 17, 2026 at 12:44 am

    you are setting Datacontexts in your mark up and in code…remove the markup and stick to one pattern

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Within my project I have a Listbox which uses a datatemplate. Within this data
I have a simple MVVM application. It contains a property, which I will change
I have a very simple MVVM Light application which has a textblock and a
Within a Listbox control I have a Data Template which consists of text and
I have pretty simple scenario (MVVM) DataTemplate visuals should change based on data I
Scenario : In a Silverlight 4 MVVM project, we have a ListBox control containing
I am starting a new MVVM project with WPF, both of which I am
I have a simple WPF application where I don't have any mvvm framework like
I'm just playing around with WPF and MVVM, and I have made a simple
I have simple class with width and height member fields which define number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.