我有一個簡單的程序,它顯示數據網格上的集合。當我運行代碼時,我看不到網格顯示數據。該模型是簡單的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;
}
你應該考慮改變兩件事...讓你的List成爲ObservableCollection,讓你的PersonList成員使用PropertyChange – 2010-06-29 14:49:51
@ Muad'Dib - 你應該讓你的評論成爲一個答案......這是唯一正確的答案。 – Stimul8d 2011-03-25 14:33:14