我有一个数据网格视图,数据网格有数量,单价和金额列。在视图中,我在文本块底部有一个文本块。文本我想要金额更改时金额列的总和(金额获得)当数量,单价改变时改变了。)
我的viewModel:
public class PurchaseOrderViewModel : WorkspaceViewModel
{
public PurchaseOrderViewModel()
{
this.OrderItems.CollectionChanged += OrderItems_CollectionChanged;
}
void OrderItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(this.OrderItems!=null && this.OrderItems.Count!=0)
{
decimal total = 0;
foreach(var item in this.OrderItems)
{
total += item.Amount;
}
this.Total = total;
}
}
#region Public Properties
private decimal _total;
public decimal Total
{
get
{
return Order.Total;
}
set
{
Order.Total = value;
RaisePropertyChanged("Total");
}
}
private ObservableCollection<ProductCartViewModel> _orderItems;
public ObservableCollection<ProductCartViewModel> OrderItems
{
get
{
if (_orderItems == null)
{
_orderItems = new ObservableCollection<ProductCartViewModel>();
}
return _orderItems;
}
set
{
_orderItems = value;
RaisePropertyChanged("OrderItems");
}
}
#endregion //Public Properties
}
但是上面的代码只有在将新行添加到集合时才更新Textblock。我想在Amount更改时立即更新Textblock。
如何做到这一点?