How to relate a collection of an observable collection in WPF

I am learning WPF, so please pose my question if he is very new! I have a set of elements and I want to associate this collection with a grid and bind Sum to a text field. Searching the Internet I found this class that will increment the event even when we make changes to the properties of the collection object.

ObservableCollection does not notice when an element in it changes (even with INotifyPropertyChanged)

But I can not get it to work in my code.

Here is my code

SaleItem

public class SaleItem : INotifyPropertyChanged
{
    public int Num { get; set; }
    public string ItemID { get; set; }
    public string Name { get; set; }

    private decimal price;
    public decimal Price
    {
        get { return price; }
        set
        {
            this.price = value;
            OnPropertyChanged("Total");
        }
    }
    public int quantity;
    public int Quantity
    {
        get { return quantity; }
        set
        {
            this.quantity = value;
            OnPropertyChanged("Total");
        }
    }

    public decimal Total
    {
        get { return decimal.Round(Price * Quantity, 2, MidpointRounding.AwayFromZero);}
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Sale

public class Sale : INotifyPropertyChanged 
{
    private Decimal _total;
    public TrulyObservableCollection<SaleItem> Items { get; set; }

    public Sale()
    {
        Items = new TrulyObservableCollection<SaleItem>();
        Items.Add(new SaleItem { ItemID = "1", Name = "Product 1", Price = 10, Quantity = 1 });
        Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });
        Items.Add(new SaleItem { ItemID = "3", Name = "Product 3", Price = 10, Quantity = 1 });

    }

    public Decimal Total
    {
        get
        { 
            return Items.Sum(x => x.Total);
        }
        set
        {
            _total = value;
            OnPropertyChanged("Total");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Mainwindow

public partial class MainWindow : Window
{
    public Sale Model { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Model = new Sale();
        this.DataContext = Model;            
    }



    private void btnQuantity_Click(object sender, RoutedEventArgs e)
    {
        Model.Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });

    }
}

Xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local ="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <local:Sale />
</Window.DataContext>

<Grid>
    <StackPanel>
        <DataGrid x:Name="grdItems" ItemsSource="{Binding Items}"></DataGrid>
        <TextBlock x:Name="txtTotal" Text="{Binding Total}"/>
        <Button x:Name="btnQuantity" Content="Update" Click="btnQuantity_Click"/>
    </StackPanel>
</Grid>

, , . Total, , - . ?

+4
2

. OnPropertyChanged, . . Sale , ​​ Total.

- NotifyCollectionChangedEvent :

Sale():

public Sale()
{
 //Instantiate your collection and add items

  Items.CollectionChanged += CollectionChanged;
}

:

public void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
  OnPropertyChanged("Total");
}

- , , , Total. , :

Items.CollectionChanged += (s,e) => OnPropertyChanged("Total");

Total Sale. setter :

public Decimal Total
{
  get
  {
    return Items.Sum(x => x.Total);
  }
}
+6

Sales.cs add:

public void AddItem(SaleItem item)
    {
        Items.Add(item);
        OnPropertyChanged("Total");
    }

MainWindow.xaml.cs:

Model.AddItem(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });

Model.Items.Add(new SaleItem { ItemID = "2", Name = "Product 2", Price = 10, Quantity = 1 });

Total.

+1

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


All Articles