更改文本框的文本时执行方法

Edw*_*win 5 c# wpf mvvm

我的界面中有这些文本框:

图片

其中Total和Change框是只读的.我的问题是,当用户输入付款时,如何执行计算变更的方法?

有界付款文本框是这样的:

private decimal _cartPayment;
public decimal CartPayment {
    get { return _cartPayment; }
    set { 
    _cartPayment = value;
    //this.NotifyPropertyChanged("CartPayment");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的XAML如下:

<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)

我的ViewModel已INotifyPropertyChanged实现,但我不知道如何从这里开始

Lou*_*ann 7

这是一个MVVM方法,它不会破解任何属性的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>
Run Code Online (Sandbox Code Playgroud)

xmlns:i作为System.Windows.Interactivityxaml 中的命名空间
ComputeNewPriceCommand是指向重新计算方法的任何类型的ICommand.


Moh*_*med 6

您可以利用UpdateSourceTrigger.你可以修改你的代码

<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)

和你的财产一样

private decimal _cartPayment;
public decimal CartPayment
{
get { return _cartPayment; }
set 
 { 
  _cartPayment = value;
  // call your required
  // method here
  this.NotifyPropertyChanged("CartPayment");
 }
}
Run Code Online (Sandbox Code Playgroud)