我想将DataTable
用于WPF DataGrid
。我实际上是在几个月前制作的示例程序中使用它,但我找不到源代码。所以,我试过了。
XAML:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,256,0,0" VerticalAlignment="Top" Height="291" Width="800">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Width="180" Header="Name" CanUserResize="False"
IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=Position}" Width="180" Header="Position" CanUserResize="False"
IsReadOnly="True" />
<DataGridTextColumn Binding="{Binding Path=E-Mail}" Width="280" Header="E-Mail" CanUserResize="False"
IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
C#:
var contacts = new DataTable();
contacts.Columns.Add("Name");
contacts.Columns.Add("Position");
contacts.Columns.Add("E-Mail");
foreach (var x in y)
{
contacts.Rows.Add(x.foo, x.bar, x.foobar);
}
dataGrid.ItemsSource = contacts.DefaultView;
答案 0 :(得分:3)
将Datagrid的 AutoGenerateColumns 属性设置为 false
[('Let', 'O'), ("'s", 'O'), ('meet', 'O'), ('on', 'O'), ('wednesday', 'DATE'), ('at', 'O'), ('09:00am', 'O'), ('.', 'O')]