我想在将该项目添加到组合框之前检查我的数组中的项目是否出现在我的组合框中,以避免重复。
我不允许使用 LINQ
代码:
private void ToonCategorien()
{
cboCategorie.Items.Clear();
foreach (String sCategorie in marrCategorie){
if (!cboCategorie.Items.Contains(sCategorie))
{
ComboBoxItem cboItem = new ComboBoxItem();
cboItem.Content = sCategorie;
cboCategorie.Items.Add(cboItem);
}
}
}
抱歉在我的 C# 代码中使用了荷兰语。
所以marrCategorie是一个数组,其中包含我从 StreamReader 读取的所有类别。问题是他无论如何都会添加所有内容。我认为这是我的 if 循环中的一个问题。
我也试过if(cboCategorie.Text.Contains(sCategorie))没有结果。
我还不允许直接发布图片,很抱歉使用超链接):
提前致谢!
解决方案,感谢博卢:
private void ToonCategorien()
{
cboCategorie.Items.Clear();
foreach (String sCategorie in marrCategorie){
if (!cboCategorie.Items.Contains(sCategorie))
{
cboCategorie.Items.Add(sCategorie);
}
}
}