简体   繁体   English

WPF-将DataGrid的DataContext绑定到DataTable

[英]WPF - Binding the DataContext of a DataGrid to DataTable

I have a DataGrid in my WPF window. 我的WPF窗口中有一个DataGrid I bind its DataContext to a System.Data.DataTable . 我将其DataContext绑定到System.Data.DataTable When I construct and fill my DataTable , the DataGrid updates and works perfectly. 当我构造并填充DataTableDataGrid更新并正常运行。 Now I want to find some elements in my DataTable and highlight their associated cells in the DataGrid . 现在,我想在DataTable找到一些元素,并在DataGrid突出显示与它们关联的单元格。 How can I do this using the MVVM pattern? 如何使用MVVM模式执行此操作? What other property of the DataGrid I should bind in order to achieve this? 为了实现这一点,我应该绑定DataGrid其他什么属性?

I have been googling for two hours and so far I got nothing. 我已经谷歌搜索了两个小时,到目前为止我什么都没得到。 Any hints would be appreciated. 任何提示将不胜感激。

The main problem was with the binding. 主要问题在于绑定。 One shall not bind the DataContext of a DataGrid to a DataTable . 不得将DataGridDataContext绑定到DataTable The better way is to bind its ItemsSource to the DataTable . 更好的方法是将其ItemsSource绑定到DataTable Although the result would appear the same. 尽管结果看起来是一样的。

Secondly, abiding to the MVVM pattern is sometimes really painful and seems unnecessarily complicated. 其次,遵守MVVM模式有时确实很痛苦,而且看起来不必要地复杂。 It seems there is no harm in doing some works programmatically. 以编程方式进行某些工作似乎没有害处

And finally, this great answer helped me a lot. 最后, 这个好答案对我有很大帮助。 I refer the future readers with the same issues to that. 我向以后的读者介绍同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM