I am attempting to list the List's items but I get repeatedly displayed the first row of the List. The List should contain three different product rows. Instead, ListView shows the same, first row three times.
我试图列出List的项目,但我反复显示List的第一行。列表应包含三个不同的产品行。相反,ListView显示相同的第一行三次。
I create list like this:
我创建这样的列表:
List<ProductsNumber> ProductsNumberList = new List<ProductsNumber>();
ProductsNumber _ProductsNumber = new ProductsNumber();
for (int i= 0; i <= otherlist.Count - 1; i++)
{
_ProductsNumber.ProdId = otherlist[i].ProdName.ToString();
_ProductsNumber.ProductsNumber = (DB_ProductList.Where(Product => Product.ProdId == otherlist[i].ProdName)).Count();
ProductsNumberList.Add(_ProductsNumber);
}
//listView
listBox.ItemsSource = ProductsNumberList;//Binding data to LISTBOX
And here is the class used to populate List:
这是用于填充List的类:
public class ProductsNumber
{
public string ProdId { get; set; }
public int ProductsNumber { get; set; }
}
If you are interested:
如果你感兴趣:
otherlist
works just fine when I list it in listview.
当我在listview中列出它时工作得很好。
DB_ProductList is defined like this:
DB_ProductList的定义如下:
ReadAllProductsList dbproduct = new ReadAllProductsList();
DB_ProductList = dbdproduct.GetAllProducts();
List<Product> ProductList = new List<Product>();
ProductList = DB_ProductList.ToList();
and it works well too. Anyway, I get correct ProductNumber in ListView. It's the ProdId that gives me problems. I get indeed correct number of iterations in ListView, but they all are the same. ListView should list all Products, and not repeat the same one.
而且效果也很好。无论如何,我在ListView中得到正确的ProductNumber。这是ProdId给我带来的问题。我在ListView中获得了正确的迭代次数,但它们都是相同的。 ListView应列出所有产品,而不是重复相同的产品。
Any clue?
I am guessing that when populating List the same record is inserted three times. But why?
我猜测在填充List时,会插入三次相同的记录。但为什么?
2 个解决方案
#1
0
Oh, I solved the mystery. Simply, moved the
哦,我解开了这个谜。简单地说,感动了
ProductsNumber _ProductsNumber = new ProductsNumber();
line inside the for
loop and it works fine now. Silly me.
for循环中的行,现在工作正常。傻我。
#2
0
Can you try write "ProductsNumber _ProductsNumber = new ProductsNumber();" into the loop?
你能尝试写“ProductsNumber _ProductsNumber = new ProductsNumber();”进入循环?
#1
0
Oh, I solved the mystery. Simply, moved the
哦,我解开了这个谜。简单地说,感动了
ProductsNumber _ProductsNumber = new ProductsNumber();
line inside the for
loop and it works fine now. Silly me.
for循环中的行,现在工作正常。傻我。
#2
0
Can you try write "ProductsNumber _ProductsNumber = new ProductsNumber();" into the loop?
你能尝试写“ProductsNumber _ProductsNumber = new ProductsNumber();”进入循环?