在使用DataGrid控件时,可以通过以下步骤将数据源绑定到DataGrid中:
<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" />
List<Person> persons = new List<Person>
{
new Person { Name = "John", Age = 25 },
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 35 }
};
dataGrid.ItemsSource = persons;
<DataGrid x:Name="dataGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
这样就可以将数据源绑定到DataGrid控件中了。