C# MVVM Calculating Total

Posted by LnDCobra on Stack Overflow See other posts from Stack Overflow or by LnDCobra
Published on 2010-06-09T17:10:28Z Indexed on 2010/06/09 17:12 UTC
Read the original article Hit count: 170

Filed under:
|
|
|

I need to calculate a trade value based on the selected price and quantity. How can

The following is my ViewModel:

class ViewModel : ViewModelBase
{
    public Trade Trade
    {
        get { return _trade; }
        set { SetField(ref _trade, value, () => Trade); }
    } private Trade _trade;

    public decimal TradeValue
    {
        get { return Trade.Amount * Trade.Price; }
    } 

}

ViewModelBase inherits INotifyPropertyChanged and contains SetField()

The Following is the Trade class:

public class Trade : INotifyPropertyChaged
{
    public virtual Decimal Amount
    {
        get { return _amount; }
        set { SetField(ref _amount, value, () => Amount); }
    } private Decimal _amount;
    public virtual Decimal Price
    {
        get { return _price; }
        set { SetField(ref _price, value, () => Price); }
    } private Decimal _price;
    ......
}

I know due to the design my TradeValue only gets calculated once (when its first requested) and UI doesn't get updated when amount/price changes. What is the best way of achieving this?

Any help greatly appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf