wpf datagrid简单显示数据(转)
wpf datagrid简单显示数据
1.在XAML代码中写出固定列,绑定数据名称
<DataGrid x:Name="DataGrid1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="249" Width="507">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="名称"/>
</DataGrid.Columns>
</DataGrid>
2.创建数据结构
3.将数据放在list中赋给datagrid
List<aaaa> infoList = new List<aaaa>();
private void Button_Click(object sender, RoutedEventArgs e)
{
aaaa ps1 = new aaaa();//单行数据
ps1.Name = "111";
infoList.Add(ps1);
ps1.Name = "222";
infoList.Add(ps1);
DataGrid1.AutoGenerateColumns = false;
DataGrid1.ItemsSource = infoList;
}
==================================================
最终效果