如何在WPF中绑定可观察集合的总和

8

我正在学习WPF,请原谅我的问题很初级!我有一组项目,我想将该组绑定到网格并将总和绑定到文本框。在网上搜索时,我发现了这个类,即使我们对集合对象的属性进行更改,它也会引发事件。

ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

但我似乎无法使其在我的代码中工作。

以下是我的代码:

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

促销

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

主窗口

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的值,那么它是正确的,但某种方式它没有在UI上更新。我错过了什么?

2个回答

10
你希望做的是,在集合本身发生更改时调用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属性不会有其他任何操作的话,你可以通过移除事件处理程序并使用lambda表达式进一步缩短代码:

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

除非您计划在Sale类中显式更新Total,否则可以删除setter

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

简短而简洁!谢谢! - Afraz Ali
我应该在某个地方解除事件绑定吗?还是让垃圾回收器来完成这项工作? - Fandi Susanto
我对商品价格变化的情况非常感兴趣,如果您知道任何好的信息,请告诉我 :) - EricG

1
在 Sales.cs 文件中添加以下内容:
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 });

这将更新总计。

感谢您的投票支持,但是@JBond提供了更好的解决方案。我的解决方案可以使用简单的List<>等,但是由于您正在使用ObservableCollection<>,因此您可以利用它的好处,例如CollectionChanged事件。 :) - aniski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接