2012-12-28 69 views
2

我想使用ObservableCollection將項目列表添加到我的Listview。 當我建,我得到這個錯誤在第2行: 「StudentCollection」 如下:WPF ObservableCollection&Listview

Inconsistent accessibility: property type System.Collections.ObjectModel.ObservableCollection<HMSystem.ChildPage1.studentData>' is less accessible than property 'SHSystem.ChildPage1.studentCollection' d:\data\visual studio 2010\Projects\SSSystem\SHSystem\ChildPage1.xaml.cs 

這裏是我到目前爲止已經試過,

// created a property 
    private ObservableCollection<studentData> _StudentCollection; 
    public ObservableCollection<studentData> StudentCollection 
    { 
     get 
     { 
      if (_StudentCollection== null) 
      { 
       _StudentCollection= new ObservableCollection<studentData>(); 
      } 
      return _StudentCollection; 
     } 
    } 

    //created a class for studentData 
    class studentData 
    { 
     public string StudentName{ get; set; } 
     public string Class{ get; set; } 
     public string Status { get; set; } 
    } 

在構造函數中:

  public ChildPage1() 
     { 
      _StudentCollection.Add(new studentData{ StudentName = "Arun", Class= "tenth", Status = "Active" }); 
     _StudentCollection.Add(new studentData{ StudentName = "Priya", Class= "ninth", Status = "Active" }); 
      InitializeComponent(); 
     } 
在XAML

 <ListView Height="96" Name="listView1" Width="226" ItemsSource="{Binding ElementName=Page1,Path=StudentCollection}"> 
      <ListView.View> 
       <GridView> 
       <GridViewColumn Width="50" Header="Name" DisplayMemberBinding="{Binding StudentName}" /> 
       <GridViewColumn Width="70" Header="Class" DisplayMemberBinding="{Binding Class}" /> 
       <GridViewColumn Width="70" Header="Status" DisplayMemberBinding="{Binding Status}" /> 
       </GridView> 
      </ListView.View> 
     </ListView> 

有什麼想法? 謝謝

回答

3

這是因爲您的收藏StudentCollection是公開的,您的類studentData是私人的。只是公開:

public class studentData 
{ 
    public string StudentName{ get; set; } 
    public string Class{ get; set; } 
    public string Status { get; set; } 
} 
+0

是的..它現在正常工作..謝謝:) – user1221765

+0

歡迎您! – JleruOHeP

相關問題