views:

10

answers:

1

I have a simple program which displays a collection on a data grid. When I run the code I do not see grid displaying data. The model is

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

The view model is

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));
    }
}

The view xaml

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

the code behind is

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

Should your DataGrid XAML contain AutoGenerateColumns="True"? I'm comparing your code with this similar detailed example.

DOK
Thats not it. I tried that. Thanks though.
Nair