简体   繁体   English

更新和删除列表视图行

[英]Update and delete listview row

Binding it to the WPF ListView, using ItemsSource="{Binding Path=ProductList}" : 使用ItemsSource="{Binding Path=ProductList}"将其绑定到WPF ListView:

   private ObservableCollection<SalesItem> _ProductList = new ObservableCollection<SalesItem>();
            public ObservableCollection<SalesItem> ProductList { get { return _ProductList; } set { _ProductList = value; } }

Insert a new row into ListView: 在ListView中插入新行:

SalesItem item = new SalesItem { Picture = product.Picture, ProductName = product.Name, Quantity = 1, TotalPrice = product.Price };
            ProductList.Add(item);

Now I want to update the quantity, and price, if the quantity is zero, remove from list : 现在,我要update the quantity, and price, if the quantity is zero, remove from list

  List<SalesItem> salesItems = (from SalesItem items in this.dgProductList.ItemsSource
                                     select items).ToList<SalesItem>();

        foreach (SalesItem salesItem in salesItems)
        {
            if (salesItem.ProductName == cbMultiProductList.Text.Trim())
            {
                // Increase item quantity
                salesItem.Quantity = salesItem.Quantity + 1;
            }
        }

Thank you. 谢谢。


EDITED: 编辑:

SalesItem is EF Code first model. SalesItem是EF代码优先模型。

Tried this, the problem is he new row will append to the last row: 尝试过此问题,问题在于新行将追加到最后一行:

ProductList.Remove(salesItem);
ProductList.Add(salesItem);

为此,您需要在SalesItem类中实现INotyfyPropertyChanged,并为每个属性进行通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM