WPF 中数据绑定和 ObservableCollections 的最佳实践是什么?
我使用 LinqToSql 作为数据源,假设我从表中查询程序员列表,然后循环遍历该数据填充 ObservableCollection。 (第一个问题,这部分是错误的吗?将数据查询到列表中然后循环遍历以填充另一个列表似乎很奇怪)
接下来,当我将程序员的 ObservableCollection 数据绑定到 ListBox 时。然后我将列表框的 selectedItem 绑定到文本框。据我所知,当我在列表框中选择一个项目时,文本框将被更新,当我在文本框中进行更改时,列表框将被更新,因此列表框绑定到的 ObservableCollection 也将被更新。
我不完全理解的是,将 OC 中的数据返回到我的原始数据源的最佳实践是什么。我是否只是循环遍历可观察集合,通过更新每条记录来提交我的更改?这看起来效率不是很高。
非常感谢您的帮助! -乔什
I am using LinqToSql as my datasource, let’s say I query a list of programmers from a table and then loop through that data populating an ObservableCollection. (First question, is this part wrong? It just seems weird to query data into a list and then loop through it to populate another list)
Next, when I databind my ObservableCollection of programmers to a ListBox. Then I bind the selectedItem of the Listbox to a TextBox. I understand that when I select an item in the ListBox the textbox will be updated and when I make a change in the textbox, the ListBox will get updated and as a result the ObservableCollection that the listbox is bound to will also be updated.
What I don’t completely understand is what the best practice is to get the data from the OC back into my original datasource. Do I just loop through the observable collection commiting my changes by updating each record? That doesn’t seem terribly efficient.
Thanks so much for your help!
-Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这纯粹是一个视图模型问题。您可以完全控制栅栏数据侧的所有对象和属性。有时,您有一个简单的安排,其中用户只能编辑单个记录,数据绑定完成所有工作,并且只需在用户单击“保存”时将其写回数据库即可。
如果要显示更多数据,并且用户可以修改其中的任何一个数据,那么在对象本身中保留一个脏标志来记录是否有任何属性已更改可能是明智的。这样,您就可以有效地仅保存修改后的条目。
由于您的对象可能支持 INotifyPropertyChanged 并且您的集合是可观察的,因此您甚至可以自动检测和管理脏标志。或者在所有设置器中将 dirty 设置为 true 可能会更简单。
无论如何,该信息甚至对用户有用。例如,用户界面可以以粗体或其他约定显示未保存的记录。
This is purely a view-model issue. You have complete control over all the objects and properties on the data side of the fence. Sometimes you have a simple arrangement where the user can only edit a single record, databinding does all the work, and you just write it back to the database when the user clicks save.
If you have a lot more data being displayed and any one piece of it can be modified by the user, then it is probably wise for you to keep a dirty flag in the object itself that records whether any of the properties have been changed. That way, you can efficiently save back just the modified entries.
Since your objects probably support
INotifyPropertyChanged
and your collections are observable, you can even automatically detect and manage the dirty flag. Or it might be simpler to just set dirty to true in all of your setters.In any case, this information can even be useful to the user. For example, a user-interface can show unsaved records in bold or with some other convention.
ObservableCollection 不是唯一可以在 wpf 中使用的集合。但它是标准集合,允许 wpf 在集合更改(例如添加或删除项目)时自动更新 ui 项目容器(如 ListBox)。你不能用 List 来做到这一点。
当用户修改 ListBoxItem 中的文本框时,ObservableCollection 不会更新,因为您不会添加、删除或重新排序集合中的项目。您更改其中一项的属性。
ListBox 包含一系列 ListBoxItem 容器,每个容器对应指定为 ItemsSource 的集合中的每一项。
每个ListBoxItem的DataContext被设置为存储在ObservableCollection中的相应项目。因此,如果您在代码中更改 TextBox 中的文本,绑定引擎将更改在绑定中为 TextBox.Text 指定的该项的属性。 ObservableCollection 对象无需更改或更新。
在此类项目属性的设置器中,通常会引发 INotifyPropertyChanged 接口的 PropertChanged 事件。这也是通常设置脏旗的地方。然后,您还可以立即从那里提交更改,保留一些脏对象列表或在提交时搜索它们,例如:
还有像 Snoop 和 WPFInspector 这样的好工具,它们极大地帮助您了解 wpf 应用程序可视化树和每个元素的数据上下文
The ObservableCollection is not the only one collection which you could use in wpf. But it is standard collection allows wpf to automatically update ui item containers like ListBox on collection changes like addition or removal of an item. You can't do it with List.
When the user modifies a textbox in ListBoxItem the ObservableCollection is not updated cause you don't add or remove or reorder items in the collection. You change the property in one of the items.
The ListBox contains a list of ListBoxItem containers, one for each item in collection specified as an ItemsSource.
The DataContext of each ListBoxItem is set to the corresponding item stored in ObservableCollection. So, if you change the text in TextBox in code, the binding engine will change the property of that item specified for TextBox.Text in binding. Nothing to change or update for the ObservableCollection object.
In the setter of such item's property the PropertChanged event of INotifyPropertyChanged interface are usually raised. That is also the usual the place to set up a dirty flag. Then you could also commit changes immediately from there, keep some list of dirty objects or search for them on commit like:
Also there are good tools like Snoop and WPFInspector, which greatly help you to understand the wpf app visual tree and datacontexts for each element