Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将集合绑定到ListView,抑制对象的重复项,但操作ListViewItem_C#_Wpf_Xaml_Uwp_.net Core - Fatal编程技术网

C# 将集合绑定到ListView,抑制对象的重复项,但操作ListViewItem

C# 将集合绑定到ListView,抑制对象的重复项,但操作ListViewItem,c#,wpf,xaml,uwp,.net-core,C#,Wpf,Xaml,Uwp,.net Core,我的问题是,在Raspberry Pi 3 Bodel B上开发一个购物系统,如下所示: 首先是一些背景: 带有条形码扫描仪的覆盆子Pi用作自助商店 员工扫描饮料,屏幕上的价格与饮料价格相加 现在应该提供一个扩展:应该显示当前扫描的所有产品的列表 到目前为止,我只实现了每个扫描的产品在ListView中显示为一个ListViewItem 这是我的模型 公共部分类产品:对象,System.ComponentModel.INotifyPropertyChanged{ 私有字符串描述符字段; pr

我的问题是,在Raspberry Pi 3 Bodel B上开发一个购物系统,如下所示:

首先是一些背景:

  • 带有条形码扫描仪的覆盆子Pi用作自助商店

    • 员工扫描饮料,屏幕上的价格与饮料价格相加
    • 现在应该提供一个扩展:应该显示当前扫描的所有产品的列表
  • 到目前为止,我只实现了每个扫描的产品在ListView中显示为一个ListViewItem

  • 这是我的模型
    公共部分类产品:对象,System.ComponentModel.INotifyPropertyChanged{
    私有字符串描述符字段;
    private System.DateTime ExpirationDateField;
    私人国际机场;
    私有字符串名称字段;
    私有十进制价格域;
    [System.Runtime.Serialization.DataMemberAttribute()]
    公共字符串描述{
    得到{
    返回此.DescriptionField;
    }
    设置{
    if((object.ReferenceEquals(this.DescriptionField,value)!=true)){
    this.DescriptionField=值;
    本.RaiseProperty变更(“说明”);
    }
    }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.DateTime过期日期{
    得到{
    返回此.ExpirationDateField;
    }
    设置{
    if((this.ExpirationDateField.Equals(value)!=true)){
    this.ExpirationDateField=值;
    本.RaiseProperty变更(“到期日”);
    }
    }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    公共整数ID{
    得到{
    返回此.IDField;
    }
    设置{
    if((此.IDField.Equals(value)!=true)){
    this.IDField=值;
    本.RaisePropertyChanged(“ID”);
    }
    }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    公共字符串名{
    得到{
    返回此.NameField;
    }
    设置{
    if((object.ReferenceEquals(this.NameField,value)!=true)){
    this.NameField=值;
    本.RaiseProperty变更(“名称”);
    }
    }
    }
    [System.Runtime.Serialization.DataMemberAttribute()]
    公共十进制价格{
    得到{
    返回此.PriceField;
    }
    设置{
    if((this.PriceField.Equals(value)!=true)){
    this.PriceField=值;
    本次提价房地产变更(“价格”);
    }
    }
    }
    公共事件System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    受保护的void RaisePropertyChanged(字符串propertyName){
    System.ComponentModel.PropertyChangedEventHandler propertyChanged=this.propertyChanged;
    如果((propertyChanged!=null)){
    propertyChanged(这是新的System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
    }
    }
    
    这个模型描述了一种可以在我们商店买到的产品

    我的XAML片段
    
    
    现在我的问题是:

    如果产品扫描两次,是否可能不添加新的
    ListViewItem
    ,而是通过在现有项目上添加相同的价格来操纵现有的
    ListViewItem

    我尝试了很多可能性,也询问了我工作中的一些开发人员,但没有人能理解

    如果你需要更多的信息,尽管问


    提前谢谢,请原谅我糟糕的英语语法:)

    因此,在@JustinXL和@user230910的大力支持下,我自己制定了一个解决方案

    我将在这里发布我的代码,这样您就可以看到我找到了一个解决方案

    XAML
    C#-收集填充

    请不要评判我的编程风格,我很快就解决了这个问题


    我希望有人可以使用我的解决方案来解决他/她的问题,感谢您的帮助,这为我节省了很多时间。

    您需要一个附加属性,如“count”,该属性最初在new上设置为1-然后每次需要添加相同的项时,您都会查看是否已经添加了该项并增加count属性,否则,您将添加一个新项目-因此“添加”代码需要执行此操作。我不太明白您所说的“如果产品扫描两次,不要添加一个新的ListViewItem,而是应该通过在现有项目上添加相同的价格来操纵现有的ListViewItem。”您能详细说明吗?就我个人而言,我将创建一个
    ProductGroup
    作为
    ListView
    的基本项模型。就像@user230910已经建议的那样,对于这个新类,您需要一个
    Count
    ,您可能还需要一个
    TotalPrice
    ,它基本上是
    Price
    *
    Count
    。这就是为什么大多数时候我们需要一个VM来扩展我们的模型类。在您的情况下,
    ProductGroup
    将像一个VM一样,帮助满足UI需求。@JustinXL正是我的意思-很好地说:)好了,现在我明白了,解释得很好。如果我找到一个可能的解决办法,我会尽我最大的努力,并在这里作出回应。
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock 
                 FontFamily="Assets/Fonts/Baskerville.ttf#Baskerville" 
                 Foreground="White">
                    <Run Text="{Binding Path=groupedProduct.Name}"/>
                    <Run Text="{Binding PriceSum,
                                Converter={StaticResource PriceConverter}, 
                                Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}"/>
                    <LineBreak/>
                    <Run Text="{Binding Path=Count,
                                Converter={StaticResource StringFormatter},
                                ConverterParameter='Anzahl: {0}',
                                Mode=TwoWay,
                                UpdateSourceTrigger=PropertyChanged}"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
    
    public class ProductGroup : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void onPropertyChanged(object sender, string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            private decimal _priceSum;
            private int _count;
            public Product groupedProduct { get; set; }
            public int Count
            {
                get
                {
                    return _count;
                }
                set
                {
                    _count = value;
                    onPropertyChanged(this, "Count");
                }
            }
            public decimal Price { get; set; }
            public decimal PriceSum
            {
                get { return _priceSum; }
                set
                {
                    _priceSum = value;
                    onPropertyChanged(this, "PriceSum");
                }
            }
        }
    
    ProductSumUp.ItemsSource = _showProducts;
    
    bool prodExists = false;
    
    foreach (ProductGroup prodz in _showProducts)
    {
         if (prodz.groupedProduct.ID == prod.ID)
            {
              prodExists = true;
            }
    }
    
    if (!prodExists)
    {
        ProductGroup prodGroup = new ProductGroup();
        prodGroup.groupedProduct = prod;
        prodGroup.Price = prod.Price;
        prodGroup.Count = 1;
        prodGroup.PriceSum += prod.Price;
        _showProducts.Add(prodGroup);
    }
     else
    {
        ProductGroup pgroup = _showProducts.First(x => x.groupedProduct.ID == prod.ID);
    
        if (pgroup != null)
        {
            pgroup.Count++;
            pgroup.PriceSum += pgroup.Price;
        }
    }