【发布时间】:2015-05-03 21:04:10
【问题描述】:
我在此应用程序的主页中设置了两个列表视图。但是使用来自 VM 的 grade 和 subject 属性的绑定无法按预期工作以显示数据列表。
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;
}
}
}
静态资源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));
}
}
}
}
项目源码树结构如下:
【问题讨论】:
-
你是如何定义静态资源“Locator”的?
-
刚刚将其添加到我上面的问题 ^^ 中,我将其添加到了我的 app.xaml。
标签: c# list listview windows-phone-8.1 mvvm-light