wpf 当文本框的文本改变时执行一个方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16708707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute a method when the textbox's text is changed
提问by Edwin
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?
其中 Total 和 Change 框是只读的。我的问题是,如何在用户输入付款时执行一种方法来计算更改?
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:
我的 XAML 如下:
<TextBox Text="{Binding Path=CartPayment, Mode=TwoWay}" />
My ViewModel has INotifyPropertyChangedimplemented, but I'm not sure how to proceed from here
我的 ViewModel 已经INotifyPropertyChanged实现,但我不知道如何从这里开始
回答by Louis Kottmann
Here is an MVVM approach that doesn't hack any properties' get/set:
这是一个 MVVM 方法,它不会破解任何属性的获取/设置:
<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:ibeing the System.Windows.Interactivitynamespace in xaml
ComputeNewPriceCommand being any kind of ICommand that points to your recalculation method.
xmlns:i作为System.Windows.Interactivityxaml
ComputeNewPriceCommand中的命名空间,它是指向您的重新计算方法的任何类型的 ICommand。
回答by Mohd Ahmed
You can take advantage of UpdateSourceTrigger. You can modify your code like
您可以利用 UpdateSourceTrigger。你可以修改你的代码,比如
<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");
}
}
回答by dkozl
What you need to do is add to your BindingUpdateSourceTrigger=PropertyChanged. By default object will be updated when control looses focus to save time.
您需要做的是添加到您的BindingUpdateSourceTrigger=PropertyChanged. 默认情况下,当控件失去焦点以节省时间时,将更新对象。
回答by greg
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;
然后,在您的 VM 中,执行以下操作;
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;
在你的 xaml 中,像这样;
<TextBox Text="{Binding Path=CartPayment,UpdateSourceTrigger=PropertyChanged}" />
Hope this helps! :)
希望这可以帮助!:)
EDIT: Take a look at the Following link. This may help you further.
编辑:看看下面的链接。这可能会进一步帮助您。

