2015-05-03 68 views
0

我已經在此應用程序的主頁中設置了兩個列表視圖。但是,使用虛擬機中的gradesubject屬性的綁定未按預期顯示數據列表。設置綁定後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:

Source Tree

+0

是如何做你所定義的靜態資源「定位」? –

+0

剛剛添加到我上面的問題^^,我將它添加到我的app.xaml。 –

回答

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> 

有了你有一部開拓創新的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

+0

這似乎已解決了VM路徑問題,但列表中的內容如下所示:http://picpaste.com/wp_ss_20150506_0002_1_-RvbeaT6P.png,任何想法如何讓組合框項目被輸出而不是財產的名稱?這是當前的VM,https://gist.github.com/BrianJVarley/956633d3a1d4c0437dc9和View,https://gist.github.com/BrianJVarley/85bb86b3c0497438f9b9 –

+0

您需要使用DataTemplate來描述您希望如何使用數據被顯示。看到這個︰https://gist.github.com/frankibem/ad4042b36e782f1be024 –

+0

原來我只是設置'DisplayMemberPath' –

2

你需要讓機器人gradessubjectspublic屬性:如下

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


    } 
} 

該項目的源代碼樹的結構。

+1

這是一個很好的發現。 – Alex

+0

好吧,沒有發現,我已經更新了兩個屬性「成績」和「科目」的訪問修飾符現在公開。但是當我在設備上運行應用程序時,列表仍然不顯示在我的視圖中。任何其他的想法,爲什麼這可能是?如果這對錯誤有任何影響,我也會在上面發佈我的模型。 –

+0

我還添加了我在app.xaml中定義Locator的方式以及我的源代碼樹的屏幕截圖供參考。 –

2

您已將視圖綁定到模型的私有屬性。數據綁定僅適用於公共屬性。

MSDN

屬性您使用的綁定源屬性的綁定必須 是你的類的公共屬性。顯式定義的接口 屬性不能用於綁定目的,也不能被保護, 私有,內部或虛擬屬性沒有實現基本的 。