4

我有一个带有设计时数据的简单 MVVM 项目,它适用于主数据和带有每个子项的用户控件的列表框。但是,当我在运行时实例化类时,主数据显示但子数据不显示(但列表框具有正确数量的项目,但它们没有任何数据显示在文本框中)。

我注意到 Sport 上的构造函数被调用了很多次,与我预期的相比(例如在运行时,我希望它只被调用两次,但它似乎被调用的次数更多)。

我有类人,和运动。每个人都可以喜欢多种运动并拥有自己喜欢的球队。我有一个从 viewModelBase 继承的 personViewModel 和 sportViewModel。

这是我的 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

        ''' <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

这是我的 wain 窗口背后的代码,它设置 MVVM 以显示 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

这是 XAML 代码

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

这是我的设计时间图像,显示了 3 个运动(根据我的加载设计时间数据)。 在此处输入图像描述

这是我的运行时间窗口,其中显示了 2 项运动(尽管它们没有数据 - 这是问题所在)。 在此处输入图像描述

4

1 回答 1

2

您正在标记和代码中设置数据上下文...删除标记并坚持一种模式

于 2013-01-09T14:33:27.713 回答