C# 当文本框';s文本已更改

C# 当文本框';s文本已更改,c#,wpf,mvvm,C#,Wpf,Mvvm,我的界面中有以下文本框: 其中合计框和更改框为只读。 我的问题是,我如何执行一个方法来计算用户在付款中键入的更改 有界付款文本框如下所示: private decimal _cartPayment; public decimal CartPayment { get { return _cartPayment; } set { _cartPayment = value; //this.NotifyPropertyChanged("CartPayment");

我的界面中有以下文本框:

其中合计框和更改框为只读。 我的问题是,我如何执行一个方法来计算用户在付款中键入的更改

有界付款文本框如下所示:

private decimal _cartPayment;
public decimal CartPayment {
    get { return _cartPayment; }
    set { 
    _cartPayment = value;
    //this.NotifyPropertyChanged("CartPayment");
    }
}
我的XAML如下所示:

<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay}" />


我的ViewModel已经实现了INotifyPropertyChanged,但是我不知道如何从这里开始,你需要做的是添加到你的绑定中。默认情况下,当控件松开焦点以节省时间时,对象将被更新。

您可以利用UpdateSourceTrigger。您可以像这样修改代码

<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

我在我的应用程序中做了类似的事情,但计算了剩余天数

您可以尝试以下方法:

//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"));
    }
然后,在您的虚拟机中,实现以下功能:

//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}" />

希望这有帮助!:)


编辑:看一看。这可能会进一步帮助您。

以下是一种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>

xmlns:i
是xaml中的
System.Windows.Interactivity
命名空间

ComputeNewPriceCommand是指向您的重新计算方法的任何类型的ICommand。

这可能有帮助吗@goodfriend,用WPF标记,不是WinformsOh,但是方法是一样的。很抱歉有这样的链接:|如果更改文本框绑定到依赖属性,比如说
ChangeValue
,那么当
\u
更改时,您只需更改
ChangeValue
,视图就会自动更新。这就是绑定的意义,对吗?@Jodrell如果你知道其他一切都错了,为什么不给他写个答案呢?现在是
xmlns:i=”http://schemas.microsoft.com/xaml/behaviors“
和nuGet软件包Microsoft.Xaml.Behaviors.Wpf