我正在制作一个基本上有列表视图和产品入口的销售点程序。当该人输入相同的产品时,该程序应该增加列表中现有产品的数量,而不是创建一个新的整行。
所以,我的问题是如何在 WPF 的 ListView 中动态添加项目。我的意思是,我想选择列表视图的特定行来获取特定的单元格。但我不想获得选定的行。只是一个特定的行。
让我将其描述为一个矩阵:lvProduct[0][2]。这应该给我第一行的第二个单元格。如何在 C# WPF 中应用它?
我的代码如下:
private void btnProductAdd_Click(object sender, RoutedEventArgs e)
{
bool addNewProductLine = true;
decimal totalSalePrice = Convert.ToDecimal(txtProductPrice.Text) * Convert.ToInt32(txtProductAmount.Text);
for (int i = 0; i < lvProducts.Items.Count; i++)
{
ProductBLL product = lvProducts.Items.GetItemAt(i) as ProductBLL;
if (product.BarcodeRetail== txtProductBarcode.Text)//If there is already the same item on the list, then confirm to add them together or not.
{
if (MessageBox.Show("There is already the same item in the list. Would you like to sum them?", "Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
product.Amount +=Convert.ToInt32(txtProductAmount.Text);
addNewProductLine = false;
break;
}
}
}
List<ProductBLL> items = new List<ProductBLL>();
items.Add(new ProductBLL() { BarcodeRetail=txtProductBarcode.Text, Name = txtProductName.Text, UnitRetail = Convert.ToInt32(cboProductUnit.SelectedIndex), SalePrice = Convert.ToDecimal(txtProductPrice.Text), Amount = Convert.ToInt32(txtProductAmount.Text), TotalSalePrice = totalSalePrice});
//lvProducts.ItemsSource = items; This code renew the listview completely. That means, it erases old datas and writes the new ones.
if (addNewProductLine == true)
{
lvProducts.Items.Add(items);
}
ClearProductEntranceTextBox();
//items[0].BarcodeRetail = "EXAMPLECODE"; This code can change the 0th row's data on the column called BarcodeRetail.
}
不幸的是,这段代码不起作用:/
先谢谢大家了!