我在我的应用程序中做了类似的事情,但计算了剩下的天数。
您可以尝试以下方法;
//Create a new class
public class ConreteAdder : IAdder
{
public decimal Add(decimal total,decimal payment)
{
return total - payment; //What ever method or mathematical solution you want
}
}
public interface IAdder
{
decimal Add(decimal total, decimal payment);
}
然后,在您的虚拟机中,实现以下内容;
private readonly IAdder _adder = new ConreteAdder();
private void NumberChanged() //Call this method within the properties you want to create the mathematical equation with
{
Change = _adder.Add(Payment, Total); //Or whatever method you want
}
public event PropertyChangedEventHandler PropertyChanged2;
private void OnResultChanged()
{
var handler = PropertyChanged2;
if (handler == null) return;
handler(this, new PropertyChangedEventArgs("Result"));
}
然后,在您的属性中,只需调用这两种方法之一。例如;
public decimal CartPayment
{
get { return _cartPayment; }
set
{
_cartPayment = value;
OnResultChanged(); //propertychanged event handler called
this.NotifyPropertyChanged("CartPayment");
}
}
在你的 xaml 中,像这样;
<TextBox Text="{Binding Path=CartPayment,UpdateSourceTrigger=PropertyChanged}" />
希望这可以帮助!:)
编辑:看看下面的链接。这可能会进一步帮助您。