There are these text boxes in my interface:
where the Total and Change boxes are readonly. My question is, how do I execute a method to calculate the change as the user types in the payment?
The bounded Payment textbox is this:
private decimal _cartPayment;
public decimal CartPayment {
get { return _cartPayment; }
set {
_cartPayment = value;
//this.NotifyPropertyChanged("CartPayment");
}
}
And my XAML is as follows:
<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay}" />
My ViewModel has INotifyPropertyChanged
implemented, but I'm not sure how to proceed from here
Here is an MVVM approach that doesn't hack any properties' get/set:
<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding ComputeNewPriceCommand}" />
</i:EventTrigger>
<i:Interaction.Triggers>
</TextBox>
xmlns:i
being the System.Windows.Interactivity
namespace in xaml
ComputeNewPriceCommand being any kind of ICommand that points to your recalculation method.
You can take advantage of UpdateSourceTrigger. You can modify your code like
<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
and your property like
private decimal _cartPayment;
public decimal CartPayment
{
get { return _cartPayment; }
set
{
_cartPayment = value;
// call your required
// method here
this.NotifyPropertyChanged("CartPayment");
}
}
What you need to do is add to your Binding
UpdateSourceTrigger=PropertyChanged
. By default object will be updated when control looses focus to save time.
I have done something similar within my application, but calculates the number of days left.
You could try something like the following;
//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);
}
Then, within your VM, implement the following;
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"));
}
Then, within your properties, just call either one of the two methods. for example;
public decimal CartPayment
{
get { return _cartPayment; }
set
{
_cartPayment = value;
OnResultChanged(); //propertychanged event handler called
this.NotifyPropertyChanged("CartPayment");
}
}
Within your xaml like so;
<TextBox Text="{Binding Path=CartPayment,UpdateSourceTrigger=PropertyChanged}" />
Hope this helps! :)
EDIT: Take a look at the Following link. This may help you further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With