设置绑定后,ListView 未填充列表

ListView isn't populated with list after setting up binding

我在此应用程序的主页中设置了两个列表视图。但是使用来自 VM 的 gradesubject 属性的绑定无法按预期显示数据列表。

MVVM light 库用于帮助遵循应用程序中的 MVVM 模式,因此如果需要使用该库以不同方式设置绑定。

我尝试通过检查以下内容来调试它,但无济于事:

有谁知道为什么列表数据在 运行 时间内没有填充到 ListView 中?

模型设置如下,包含两个列表,我需要绑定到视图中的列表视图:

namespace LC_Points.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            //call methods to initilise list data
            GetGradeTypes();
            GetSubjectTypes();
        }


        private List<Grade> grades { get; set; }
        private List<Grade> subjects { get; set; }


        public void GetGradeTypes()
        {
            List<Grade> gradeList = new List<Grade>();

            // Adding Grades to List
            gradeList.Add(new Grade { grade = "A1" });
            gradeList.Add(new Grade { grade = "A2" });
            gradeList.Add(new Grade { grade = "B1" });
            gradeList.Add(new Grade { grade = "B2" });
            gradeList.Add(new Grade { grade = "B3" });
            gradeList.Add(new Grade { grade = "C1" });
            gradeList.Add(new Grade { grade = "C2" });
            gradeList.Add(new Grade { grade = "C3" });
            gradeList.Add(new Grade { grade = "D1" });
            gradeList.Add(new Grade { grade = "D2" });
            gradeList.Add(new Grade { grade = "D3" });
            gradeList.Add(new Grade { grade = "E,F,NG" });

            grades = gradeList;

        }


        public void GetSubjectTypes()
        {
            List<Grade> subjectList = new List<Grade>();

            // Adding Subjects to List
            subjectList.Add(new Grade { subject = "Accounting" });
            subjectList.Add(new Grade { subject = "Agricultural Economics" });
            subjectList.Add(new Grade { subject = "Agricultural Science" });
            subjectList.Add(new Grade { subject = "Ancient Greek" });
            subjectList.Add(new Grade { subject = "Applied Math" });
            subjectList.Add(new Grade { subject = "Biology" });
            subjectList.Add(new Grade { subject = "Business" });
            subjectList.Add(new Grade { subject = "Business Group" });
            subjectList.Add(new Grade { subject = "Chemistry" });
            subjectList.Add(new Grade { subject = "Classical Studies" });
            subjectList.Add(new Grade { subject = "Engineering" });
            subjectList.Add(new Grade { subject = "English" });

            subjects = subjectList;

        }

    }
}

静态资源Locator在App.xaml中定义如下:

<Application.Resources>
        <vm:ViewModelLocator xmlns:vm="using:LC_Points.ViewModel" x:Key="Locator" />
    </Application.Resources>

这是包含两个列表视图的视图:

<Page x:Class="LC_Points.MainPage"
      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:local="using:LC_Points"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      DataContext="{Binding Source={StaticResource Locator},
                            Path=MainViewModel}"
      mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="90*" />
            <RowDefinition Height="10*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListView x:Name="subjectOneLbx"
                  Grid.ColumnSpan="2"
                  Width="211"
                  Height="48"
                  Margin="10,159,0,368.833"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding subjects}" />

        <ListView x:Name="gradeOneLbx"
                  Grid.Column="1"
                  Grid.ColumnSpan="2"
                  Width="49"
                  Height="48"
                  Margin="132.667,159,0,368.833"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding grades}" />
    </Grid>
</Page>

这是应用程序的数据模型:

namespace LC_Points.Model
{
    public class Grade : INotifyPropertyChanged
    {

        // The name of the subject.
        public string subject { get; set; }

        // The type of Grade, eg, A, B2 etc..
        public string grade { get; set; }

        // The points paired with each grade type.
        public int points { get; set; }


        private int _count;
        public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
                RaisePropertyChanged("Count");
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}

项目源码树结构如下:

您需要使机器人 gradessubjects 成为 public 属性。

您已将视图绑定到模型的私有属性。数据绑定仅适用于 public 属性。

MSDN:

The properties you use as binding source properties for a binding must be public properties of your class. Explicitly defined interface properties cannot be accessed for binding purposes, nor can protected, private, internal, or virtual properties that have no base implementation.

我之前没有使用过 mvvm-light class,所以我无法对此发表评论。但是,请考虑对 App.xaml.

的这些更改

案例 1:将 App.xaml 更改为此并更改 MainViewModel 中的名称空间:

<Application
    x:Class="LC_Points.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LC_Points">

    <Application.Resources>
        <local:MainViewModel x:Key="Locator"/>
    </Application.Resources>

</Application>

MainViewModel.cs:

namespace LC_Points
{
    /// <summary>
    /// ...
    /// </summary>
    public class MainViewModel
    {
        /// <summary>
        /// ...
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()

情况 2:保持 MainViewModel 不变,然后更改 App.xaml

<Application
    x:Class="LC_Points.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LC_Points.ViewModel">

    <Application.Resources>
        <local:MainViewModel x:Key="Locator"/>
    </Application.Resources>

</Application>

使用您拥有的原始 xaml,内容几乎不会显示,并且由于您没有提供数据模板,您将获得 LC_Points.Model.Grade 成绩和科目中的每个条目。所以,我改成这样(可能不是你想要的,只是为了说明):

<Page
    x:Class="LC_Points.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LC_Points"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    DataContext="{Binding Source={StaticResource Locator}}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="90*" />
            <RowDefinition Height="10*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListView x:Name="subjectOneLbx" Grid.RowSpan="2" Margin="10" HorizontalAlignment="Left"
                  ItemsSource="{Binding subjects}" SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding subject}" Margin="5"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <ListView x:Name="gradeOneLbx" Grid.Column="1" Margin="10" HorizontalAlignment="Left"
                  ItemsSource="{Binding grades}" SelectionMode="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding grade}" Margin="5"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

然后我得到了下面的结果