WPF 中的 Datagrid 和 sql 数据表与其他方法
这是我尝试做的。我想在数据网格内提供产品的就地编辑。首先,我将 List
包装到 ObservableCollection 中,并为每个 POCO 实现了 INotifyPropertyChanged 接口。撤消更改并跟踪已更改的内容以提交似乎很困难,而且我遇到了很多问题。而且我觉得在属性更改时为每个 poco 创建如此多的事件处理程序并不好奇怪...
所以我想问 DataTable 是否解决了这些问题?即使它解决了验证问题?它对 POCO 一无所知......还有其他更好的解决方案吗?
Here is what i tried to do.. I want to provide an in place editing of products within a datagrid. Firstly i wrapped into a ObservableCollection a List<Product>
and implemented INotifyPropertyChanged interface for each POCO. Undoing changes and tracking what has changed in order to commit seems hard and i had lots of problems.. Also i feel that creating so many event handlers for every single poco when a property changed is not good strange...
So i am asking does DataTable solve these problems ? Even it solves what about validation ?It doesnt know anything about the POCO.... Are any other better solutions for this ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性更改处理程序并没有那么糟糕。下面是一个示例:
就我个人而言,我更希望让每个
Product
跟踪它自己的更改,而不是在 ViewModel 中跟踪它们。首次创建产品时,保留原始数据的副本,并提供类似UndoChanges()
方法的方法,该方法只需重新加载原始数据。要跟踪各个属性的更改,我将在每个
Product
属性的set
方法中执行此操作。这是因为PropertyChanged
事件可以手动引发,并不一定意味着属性已更改。The property change handlers aren't that bad. Here's an example:
Personally, I would prefer to have each
Product
track it's own changes, instead of tracking them in the ViewModel. When a product first gets created, keep a copy of the original data, and provide something like aUndoChanges()
method that simply reloads the original data.To track changes on individual properties, I would do that in the
set
method of eachProduct
property. This is because thePropertyChanged
event can be raised manually, and doesn't necessarily mean that the property has changed.DataTable 确实解决了一些问题......
DataView
)进行排序和过滤,可以更快地产生结果。话虽如此……
Bindings
进行验证很容易实现。所以最终这是你的选择。不过,我不介意可观察的集合和 INotifyPropertyChanged 通知,因为它们似乎充分利用了 WPF DataGrid...样式和性能方面。
DataTable do solve a few things...
DataView
.Having said that ...
INotifypropertyChanged
.Bindings
are easily achievable.So ultimately its your choice. However I dont mind observable collections and INotifyPropertyChanged notifications as they seem to get the best out of WPF DataGrid... styling and performance wise.