如何在WPF中绑定Observable集合的总和

Afr*_*Ali 6 c# data-binding wpf xaml

我正在学习WPF,所以如果它是非常新手,请提出我的问题!我有一个Items集合,我想将该集合绑定到Grid并将Sum绑定到文本框.在线搜索我发现这个类会引发事件,即使我们对集合对象的属性进行了更改.

ObservableCollection没有注意到它中的Item何时发生变化(即使使用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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

拍卖

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

主窗口

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

    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

当我点击按钮并更新网格中的项目数量时,我正在尝试测试添加项目.如果我设置了一个断点并看到Total的值,那么它是正确的但不知何故它没有在UI上更新.我在这里错过了什么?

JBo*_*ond 8

你想做什么.是OnPropertyChanged在集合本身被更改时调用.重新计算总数.目前,Sale该类不知道该集合已更新,并且需要更新其Total属性.

你可以这样做的一种方法是检查NotifyCollectionChangedEvent这样的:

在Sale()的构造函数中:

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

  Items.CollectionChanged += CollectionChanged;
}
Run Code Online (Sandbox Code Playgroud)

然后添加事件的处理程序:

public void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
  OnPropertyChanged("Total");
}
Run Code Online (Sandbox Code Playgroud)

如果除了更新Total属性之外,当集合发生变化时,你永远不会做任何其他事情.您可以通过删除事件处理程序并使用lambda表达式来进一步缩短代码:

Items.CollectionChanged += (s,e) => OnPropertyChanged("Total");
Run Code Online (Sandbox Code Playgroud)

除非你曾经计划在课堂上明确更新Total自己Sale.该setter可以将其删除:

public Decimal Total
{
  get
  {
    return Items.Sum(x => x.Total);
  }
}
Run Code Online (Sandbox Code Playgroud)