简单的silverlight示例不会在datagrid上显示我的数据

时间:2010-06-29 14:47:05

标签: data-binding datagridview silverlight-4.0

我有一个简单的程序,它在数据网格上显示一个集合。当我运行代码时,我没有看到网格显示数据。该模型是

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 个答案:

答案 0 :(得分:1)

您的DataGrid XAML是否应包含AutoGenerateColumns="True"? 我正在将您的代码与此类detailed example进行比较。

答案 1 :(得分:1)

我绑定的集合不公开。那是我的问题。

相关问题