2010-06-29 78 views
1

我有一個簡單的程序,它顯示數據網格上的集合。當我運行代碼時,我看不到網格顯示數據。該模型是簡單的silverlight示例不顯示我的datagrid數據

public class Person 
{ 
    public string Name; 
    public int Age; 
    public bool Sex; 
} 

視圖模型是

public class PeopleViewModel:INotifyPropertyChanged 
{ 
    List<Person> _personList; 

    public PeopleViewModel() 
    { 
     _personList = new List<Person>(); 
     _personList.Add(new Person() { Name = "n1", Age = 20, Sex = true }); 
     _personList.Add(new Person() { Name = "n2", Age = 20, Sex = true }); 
     _personList.Add(new Person() { Name = "n3", Age = 20, Sex = true }); 
     _personList.Add(new Person() { Name = "n4", Age = 20, Sex = true }); 
     _personList.Add(new Person() { Name = "n5", Age = 20, Sex = false }); 
    } 

    public List<Person> PersonList 
    { 
     get { return _personList; } 
     set { _personList = value; } 
    } 


    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); 
    } 
} 

視圖XAML

<Grid x:Name="LayoutRoot" Background="White"> 
    <sdk:DataGrid Name="dataGrid1"> 
    </sdk:DataGrid> 
</Grid> 

後面的代碼是

public PeopleView() 
    { 
     InitializeComponent(); 
     PeopleViewModel model = new PeopleViewModel(); 
     dataGrid1.ItemsSource = model.PersonList; 
    } 
+2

你應該考慮改變兩件事...讓你的List成爲ObservableCollection,讓你的PersonList成員使用PropertyChange – 2010-06-29 14:49:51

+0

@ Muad'Dib - 你應該讓你的評論成爲一個答案......這是唯一正確的答案。 – Stimul8d 2011-03-25 14:33:14

回答

1

我綁定的集合並不公開。那是我的問題。

1

如果你的DataGrid的XAML包含AutoGenerateColumns="True"? 我將您的代碼與此類似的detailed example進行比較。

+0

那不是。我試過了。不過謝謝。 – Nair 2010-06-29 16:14:31