通过ObservableCollection 绑定到 DataGrid.
1. 前台Xaml.
<DataGrid x:Name="dgMeasurements" HorizontalAlignment="Left" Margin="10,69,0,10" ItemsSource="{Binding}" AutoGenerateColumns="False" Width="370"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Width="90" Binding="{Binding Name}" /> <DataGridTextColumn Header="Unit" Width="40" Binding="{Binding Unit}" /> <DataGridTextColumn Header="Value" Width="80" Binding="{Binding Value}" /> <DataGridTextColumn Header="LowerLimit" Width="80" Binding="{Binding LowerLimit}" /> <DataGridTextColumn Header="UpperLimit" Width="80" Binding="{Binding UpperLimit}" /> </DataGrid.Columns> </DataGrid>
2. 后台Code
ObservableCollection 如果是Struct的话,则不能正确的显示。原因未知,有空仔细研究一下。
参考: http://www.cnblogs.com/gnielee/archive/2010/04/16/wpf4-datagrid-control-basic.html
ObservableCollection<TestMeasurements> measurements = new ObservableCollection<TestMeasurements>(); //measurements.Add(new TestMeasurements()... dgMeasurements.DataContext = measurements;