在WPF System.Windows.Data中使用DataTable和DataGrid时出现错误
我使用WPFToolkit的DataGrid来显示一些数据。
DataTable 在 myfile.xaml.cs 中初始化,
myTable = new DataTable();
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.Int64");
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Name";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);
依此类推。
正如此处建议的,我
myGrid.ItemsSource = myTable.DefaultView;
在 myfile.xaml.cs 中使用。
在 myfile.xaml 中,我只定义了
<my:DataGrid Name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"/>
当我向表中添加条目时,
DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";
Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);
条目正确添加到 GUI 中的网格,但是我收到以下错误:
System.Windows.Data Error: 39 : BindingExpression path error: 'ID' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=ID; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
那么如何修复此错误? 有什么提示吗?
谢谢。
I use DataGrid of WPFToolkit to display some data.
The DataTable is initialized in myfile.xaml.cs with
myTable = new DataTable();
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.Int64");
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Name";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);
and so on.
As suggested here I use
myGrid.ItemsSource = myTable.DefaultView;
in myfile.xaml.cs.
In myfile.xaml I have only defined
<my:DataGrid Name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"/>
When I add an entry to the table with
DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";
Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);
the entry is added correctly to the grid in the GUI, however I receive the following error:
System.Windows.Data Error: 39 : BindingExpression path error: 'ID' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=ID; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
So how can I fix this error?
Any hints?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这与您执行代码的顺序有关。尝试将其更改
为:
即:在将项目添加到行之前将行添加到数据表中
I think this has something to do with the order in which you execute your code. Try changing this:
into this:
i.e. : add your row to the dataTable BEFORE you add the items to the row