如何防止在WPF C#中添加重复项listview gridview? [英] How to prevent adding duplicate items listview gridview in WPF C#?
问题描述
我有ListView和GridView(列说IteName,DataType,Value)
现在我想将项目添加到listview但是如果Item已经出现在Listview中那么我必须给出说出Item的消息已经在场了
我试过但没有得到任何解决方案,
请帮我摆脱这个。
谢谢
Tink
我试过的:
I have ListView with GridView(column say IteName, DataType, Value)
now I want to add item to listview But If Item is already present in Listview then I have to give message that say Item already present
I tried but not getting any solution,
Please help me to get out from this.
Thanks
Tink
What I have tried:
foreach (dynamic item in lstSusbscribedGroupTag.Items)
{
if (if(lstSubscribeItem.Items.Contains(Item.ToString())))
continue;
else
selectedItemCollection.Add(item);
}
推荐答案
使用列出的查找(xxx)方法之一搜索ListView以查看项目是否已存在在文档 [ ^ ]。
Search the ListView to see if the item already exists, using one of the Find(xxx) methods listed in the documentation[^].
另一个简单的解决方案是使用 System.Linq;
:
Another simple solution is to use System.Linq;
:
foreach (var item in lstSusbscribedGroupTag.Items.Distinct())
{
selectedItemCollection.Add(item);
}
如果项目属于复杂类型,那么您需要做更多的工作。以下是两种不同的解决方案:
1. Enumerable.Distinct(TSource)方法(IEnumerable(TSource))(System.Linq) [ ^ ](例子#2)
2 。 c# - LINQ在特定属性上的Distinct() - Stack Overflow [ ^ ]
If the items are of a complex type, then you need to do a little bit more work. Here are two different solutions for you:
1. Enumerable.Distinct(TSource) Method (IEnumerable(TSource)) (System.Linq)[^] (example #2)
2. c# - LINQ's Distinct() on a particular property - Stack Overflow[^]
这篇关于如何防止在WPF C#中添加重复项listview gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!