在使用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控件中了。