Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a method when the textbox's text is changed

Tags:

c#

mvvm

wpf

There are these text boxes in my interface:

Image

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

like image 896
Edwin Avatar asked May 23 '13 08:05

Edwin


4 Answers

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.

like image 126
Louis Kottmann Avatar answered Nov 06 '22 10:11

Louis Kottmann


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");
 }
}
like image 27
Mohd Ahmed Avatar answered Nov 06 '22 12:11

Mohd Ahmed


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.

like image 36
dkozl Avatar answered Nov 06 '22 10:11

dkozl


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.

like image 1
greg Avatar answered Nov 06 '22 10:11

greg