ObservableCollection 中的 WPF 唯一条目
我有一个 Customers 类的 ObservableCollection,是从数据库查询中填充的。由于表格的连接方式,我有时会得到一次性条目(比尔的送货地址与他的邮寄地址不同),这些条目在数据中显示两次。
每个客户行都有一个 ID 作为唯一主键,当有人从绑定的 ListView 中选择一行时,我用它来提取更多客户信息。 在此程序的 WinForms 版本中,我将在 ListView 中搜索 CustomerID,如果找到,我将避免再次插入它。
ObservableCollection 似乎无法轻松地告诉我 CustomerID 是否已存在于集合的类实例之一中,因此我想知道处理此问题的最佳方法是什么。
到目前为止我的想法:
' Not sure how to make this work, since the CustomerID and Name would be the same, but the city, state, zip might not be.'
t = new classCustomer(CustomerID, CustomerName, City, State, Zip)
if not sr.contains(t) then
sr.Add(t)
end if
可能弄清楚如何创建 ObservableDictionary,但到目前为止所有示例都是用 C# 编写的,我可能需要一段时间才能将其移植到 VB.net
有人知道更好的实施吗?
I have an ObservableCollection of class Customers that I fill from a database query. Because of how the tables are joined, I sometimes get one off entries (Bill has a shipping address that is different than his mailing address) that show up in the data twice.
Each customer row has an ID as unique primary key, and this is what I use to pull up more customer information when someone selects a row from the bound ListView.
In the WinForms version of this program, I would search the ListView for the CustomerID, and if it was found I would avoid inserting it a second time.
ObservableCollection doesn't seem to have the ability to easily tell me if a CustomerID already exists in one of the class instances of the collection, so I'm wondering what the best way to handle this is.
The ideas I've had so far:
' Not sure how to make this work, since the CustomerID and Name would be the same, but the city, state, zip might not be.'
t = new classCustomer(CustomerID, CustomerName, City, State, Zip)
if not sr.contains(t) then
sr.Add(t)
end if
Possibly figure out how to create an ObservableDictionary, but so far all the examples are in C#, and it may take me a while to port it over to VB.net
Anyone know of a better implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需告诉 .NET 一个人的定义是什么,在本例中是 ID。
只需在您的客户对象上覆盖 Equals,那么您的集合现在就能够知道 2 个客户是否相等:
如果没有覆盖,它不知道如何判断它们是否相等。
You simply need to tell .NET what defines a person, in this case the ID.
Simply override Equals on your customer object, then your collection will now be able to know if 2 customers are equivalent:
without the override it does not know how to tell if they are equivalent or not.