我已經在此應用程序的主頁中設置了兩個列表視圖。但是,使用虛擬機中的grade
和subject
屬性的綁定未按預期顯示數據列表。設置綁定後ListView沒有填充列表
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:
是如何做你所定義的靜態資源「定位」? –
剛剛添加到我上面的問題^^,我將它添加到我的app.xaml。 –