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

时间:2015-05-03 21:04:11

标签: c# list listview windows-phone-8.1 mvvm-light

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

MVVM光库用于帮助跟踪应用程序中的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;

        }

    }
}

静态资源定位器在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>

这是App的数据模型:

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));
            }
        }


    }
}

项目源树的结构如下:

Source Tree

3 个答案:

答案 0 :(得分:2)

您需要将机器gradessubjects设为public属性。

答案 1 :(得分:2)

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

MSDN

  

您用作绑定的绑定源属性的属性必须   是你班级的公共财产。明确定义的接口   无法访问属性用于绑定目的,也不能保护,   没有基础的私有,内部或虚拟属性   实施

答案 2 :(得分:1)

之前我没有使用过mvvm-light类,所以我不能对此发表评论。但是,请考虑对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>

使用你拥有的orignal 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>

我获得了以下结果 Image showing result