Associating a collection with a ListView, suppressing duplicate objects, but controlling ListViewItem

My problem is, developing a shopping system for raspberries Pi 3 Bodel B, is as follows:

First of all, some context:

  • Raspberry Pi with a barcode scanner used as a self-service store

    • Employees view the drink, and the price on the screen adds the price of the drink.
    • Now you need to provide an extension: a list of all scanned products should be displayed.
  • So far, I have only ensured that each scanned product is displayed as one ListViewItem in the ListView

This is my model

    public partial class Product : object, System.ComponentModel.INotifyPropertyChanged {
    private string DescriptionField;
    private System.DateTime ExpirationDateField;
    private int IDField;
    private string NameField;
    private decimal PriceField;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Description {
        get {
            return this.DescriptionField;
        }
        set {
            if ((object.ReferenceEquals(this.DescriptionField, value) != true)) {
                this.DescriptionField = value;
                this.RaisePropertyChanged("Description");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public System.DateTime ExpirationDate {
        get {
            return this.ExpirationDateField;
        }
        set {
            if ((this.ExpirationDateField.Equals(value) != true)) {
                this.ExpirationDateField = value;
                this.RaisePropertyChanged("ExpirationDate");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public int ID {
        get {
            return this.IDField;
        }
        set {
            if ((this.IDField.Equals(value) != true)) {
                this.IDField = value;
                this.RaisePropertyChanged("ID");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Name {
        get {
            return this.NameField;
        }
        set {
            if ((object.ReferenceEquals(this.NameField, value) != true)) {
                this.NameField = value;
                this.RaisePropertyChanged("Name");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public decimal Price {
        get {
            return this.PriceField;
        }
        set {
            if ((this.PriceField.Equals(value) != true)) {
                this.PriceField = value;
                this.RaisePropertyChanged("Price");
            }
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

This model describes a product that you can buy in our store.

My XAML Snippet

<ListView Name="ProductSumUp" 
          AllowDrop="False"  
          SelectionMode="None" 
          CanDrag="False" 
          CanReorderItems="False" 
          CanDragItems="False" 
          Grid.Column="2" 
          HorizontalAlignment="Left" 
          Height="290" 
          Margin="10,10,0,0" 
          Grid.Row="1" 
          VerticalAlignment="Top" 
          Width="180" 
          RenderTransformOrigin="1.682,0.59" 
          Foreground="White" 
          FontFamily="Assets/Fonts/Baskerville.ttf#Baskerville">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock FontFamily="Assets/Fonts/Baskerville.ttf#Baskerville" 
                               Foreground="White">
                        <Run Text="{Binding Name}"/>
                        <Run x:Name="{Binding Price}"/>
                        </TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Now my question is:

, , ListViewItem, ListViewItem .

, , .

, .

:)

+4
1

, @JustinXL @user230910.

, , .

XAML

<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");
            }
        }
    }

# - CollectionFill

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;
    }
}

result

, , .

, - / , .

+2

Source: https://habr.com/ru/post/1682405/


All Articles