0

I am trying to populate a datagrid from a list of PersonModel

My ViewModel

public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel()
        {
            Persons = new List<PersonModel>();
            PersonModel Hilto = new PersonModel("Hilto",22);
            Persons.Add(Hilto);
            PersonModel George = new PersonModel("George",60);
            Persons.Add(George);
            PersonModel Bill = new PersonModel("Bill",4);
            Persons.Add(Bill);
        }

            private List<PersonModel> _person;
        public List<PersonModel> Persons
        {
            get { return _person; }
            set { SetProperty(ref _person, value); }
        }
    }
}

and My View

<Window x:Class="Datagrid.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True">

    <Grid>
        <DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="True"/>
    </Grid>
</Window>

But when I run my code I get a datagrid with three blank rows.

if I remove the AutoGenerateColumns and define the columns in the xaml, I get the Columns and the three rows but no data in the columns.

PersonModel

  public class PersonModel
    {
        public PersonModel(string n, int a)
        {
            Name = n;
            Age = a;
        }
        public string Name;
        public int Age;
    }
Hilto
  • 21
  • 4

0 Answers0