【发布时间】:2010-06-29 14:47:05
【问题描述】:
我有一个简单的程序,它在数据网格上显示一个集合。当我运行代码时,我看不到显示数据的网格。型号是
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
-
@Muad'Dib - 你应该把你的评论作为答案......因为它是唯一正确的。
标签: data-binding datagridview silverlight-4.0