ListView isn't populated with list after setting up binding

I've set up two list views in the main page of this application. But the binding using the grade and subject properties from the VM, isn't working as expected to display the list of data.

The MVVM light library is being used to aid in following the MVVM pattern in the application, so if the binding needs to be set up differently using that library.

I tried debugging this by checking the following but to no avail:

  • Check data context is set up
  • Check properties names are correct and correct syntax for binding

Does anyone know why the list data isn't being populated in the ListView during run time?

The model is set up as follows, containing two lists that I need to tie to the list views in the View:

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;

        }

    }
}

The static resource Locator is defined as follows in App.xaml:

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

This is the View with the two list views:

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

And this is the data Model for the 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));
            }
        }


    }
}

The project source tree is structured as follows:

Answers


I haven't used the mvvm-light class before and so I can't comment on that. However, consider these alterations to App.xaml.


Case 1: Change App.xaml to this and alter the namespace in 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()

Case 2: Leave MainViewModel as is, and change 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>

With the orignal xaml that you have, the content will barely show and since you've not provided a datatemplate, you'll get LC_Points.Model.Grade for each entry in grades and subjects. So, I changed it to this (may not be what you want, just for illustration):

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

And I obtained the following result


You need to make bot grades and subjects to be public properties.


You have bound the view to the private properties of the model. Data Binding works with public properties only.

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.


Need Your Help

ADFS freshness and session sliding

c# asp.net session wif adfs2.0

I have implemented session sliding using in my customehttphandler module.

How to config devise to make user login after register even without confirm email via email link?

ruby-on-rails ruby devise

I use devise (2.0.4) to setup user register and sign-in functions. Now I want to make user be able to sign in after sign up. Event a confirmation email send to his email box. Just mark "This user i...