前台Code
1 <DataGrid Name="DataGrid1" AutoGenerateColumns="False" IsReadOnly="True" Width="550"> 2 <DataGrid.Columns> 3 <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/> 4 <DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="100"/> 5 <DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="100"/> 6 <DataGridTextColumn Header="Date" Binding="{Binding Date}" Width="200"/> 7 </DataGrid.Columns> 8 </DataGrid> 9 10
后台
1 public class Person 2 { 3 public String Name { get; set; } 4 public int Age { get; set; } 5 public String Address { get; set; } 6 public DateTime Date { get; set; } 7 } 8 /// <summary> 9 /// MainWindow.xaml 的交互逻辑 10 /// </summary> 11 public partial class MainWindow : Window 12 { 13 public MainWindow() 14 { 15 InitializeComponent(); 16 var persons = new List<Person>(); 17 for (var i = 0; i < 5; i++) 18 { 19 persons.Add(new Person { Name = "Person " + i.ToString(), Age = 20 + i, Address = "Address", Date=DateTime.Now }); 20 } 21 DataGrid1.ItemsSource = persons; 22 } 23 }