前台Code
<DataGrid Name="DataGrid1" AutoGenerateColumns="False" IsReadOnly="True" Width="550">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
<DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="100"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="100"/>
<DataGridTextColumn Header="Date" Binding="{Binding Date}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
后台
public class Person
{
public String Name { get; set; }
public int Age { get; set; }
public String Address { get; set; }
public DateTime Date { get; set; }
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var persons = new List<Person>();
for (var i = 0; i < 5; i++)
{
persons.Add(new Person { Name = "Person " + i.ToString(), Age = 20 + i, Address = "Address", Date=DateTime.Now });
}
DataGrid1.ItemsSource = persons;
}
}