У меня есть простая программа, которая отображает набор на сетке данных. Когда я выполняю код, я не вижу, что сетка отображает данные. Модель
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;
}
Должен ли ваш DataGrid XAML содержать AutoGenerateColumns = "True"
?
Я сравниваю ваш код с этим похожим подробным примером .