wpf 禁用触发 TextChanged 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17324057/
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
Disable firing TextChanged event
提问by Ms. Nobody
I have textboxand I'm changing the text inside it when lostFocusis fired but that also fires up the textChangedevent, which I'm handling but I don't want it to be fired in this one case, how can I disable it here?
我有文本框,我在lostFocus触发时更改其中的文本,但这也会触发textChanged我正在处理的事件,但我不希望在这种情况下触发它,如何在此处禁用它?
UPDATE:
更新:
The idea with boolis good but I have couple of textboxes and I use the same event for all of them, so it's not working exactly as I want the way I want.
的想法bool很好,但我有几个文本框,并且我对所有文本框都使用相同的事件,所以它并没有完全按照我想要的方式工作。
Now it's working! :
现在它开始工作了!:
private bool setFire = true;
private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
{
if (this.IsLoaded)
{
System.Windows.Controls.TextBox textbox = sender as System.Windows.Controls.TextBox;
if(textbox.Text.ToString().Contains('.'))
{
textbox.Foreground = new SolidColorBrush(Colors.Gray);
textbox.Background = new SolidColorBrush(Colors.White);
setFire = false;
textbox.Text = "something else";
setFire = true;
}
}
}
private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
{
if ((this.IsLoaded) && setFire)
{
System.Windows.Controls.TextBox textbox = sender as System.Windows.Controls.TextBox;
if(textbox.Text.ToString().Contains('.'))
{
textbox.Foreground = new SolidColorBrush(Colors.White);
textbox.Background = new SolidColorBrush(Colors.Red);
}
}
setFire = true;
}
I managed to put the boolback on trueafter editing the text and it works so thx guys :]
在编辑文本后,我设法把它bool放回原处true,它的工作原理非常感谢伙计们:]
回答by JtM
Just remove the event handler and then add it after you've done what you need to.
只需删除事件处理程序,然后在完成所需操作后添加它。
private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
{
this.mytextbox.TextChanged -= this.myTextBox_TextChanged;
if(textbox.Text.ToString().Contains('.'))
{
textbox.Foreground = new SolidColorBrush(Colors.Gray);
textbox.Background = new SolidColorBrush(Colors.White);
}
this.mytextbox.TextChanged += this.myTextBox_TextChanged;
}
回答by Tigran
Simpliest way that I can think of is using conditnional boolvariable.
When you are going to set the text on LostFocusset it to trueand inside textChangedevent handler check if that boolvariable is true, do not do nothing.
我能想到的最简单的方法是使用条件bool变量。当您要设置文本时,LostFocus将其设置为true并在textChanged事件处理程序内部检查该bool变量是否为true,不要什么都不做。
回答by user1812146
I feel.. We can do this in Best way and easy way..!
我觉得..我们可以以最好的方式和简单的方式做到这一点..!
//My textbox value will be set from other methods
txtNetPayAmount.Text = "Ravindra.pc"; //I wanted to avoide this to travel in my complex logic in TextChanged
private void txtNetPayAmount_TextChanged(object sender, EventArgs e)
{
_strLoanPayment = Convert.ToString(txtNetPayAmount.Text).Trim();
if (string.IsNullOrEmpty(_strLoanPayment)) return;
if(!_isPayAmountEntered) return;
//Some logic.. Avoided to run each time on this text change
}
private bool _isPayAmountEntered = false;
private void txtNetPayAmount_Enter(object sender, EventArgs e)
{
_isPayAmountEntered = true;
}
private void txtNetPayAmount_Leave(object sender, EventArgs e)
{
_isPayAmountEntered = false;
}
private void txtNetPayAmount_KeyPress(object sender, KeyPressEventArgs e)
{
_isPayAmountEntered = false;
}
回答by Fabian Bigler
If you have multiple Textboxes, just use a List of String where you store the textboxes whose state is DoNotFire.
如果您有多个文本框,只需使用一个字符串列表,您可以在其中存储状态为DoNotFire.
Your updated code (Also improved other things):
您更新的代码(还改进了其他方面):
private List<string> doNotFireTextBoxes = new List<string>();
private void mytextbox_LostFocus(object sender, RoutedEventArgs e)
{
if (this.IsLoaded)
{
System.Windows.Controls.TextBox textbox = (System.Windows.Controls.TextBox) sender;
doNotFireTextBoxes.Add(textbox.Name)
if(textbox.Text.Contains('.'))
{
textbox.Foreground = new SolidColorBrush(Colors.Gray);
textbox.Background = new SolidColorBrush(Colors.White);
}
}
}
private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
{
if (this.IsLoaded)
{
System.Windows.Controls.TextBox textbox = (System.Windows.Controls.TextBox) sender;
if(!doNotFireTextBoxes.Contains(textbox.Name))
{
if(textbox.Text.Contains('.'))
{
textbox.Foreground = new SolidColorBrush(Colors.White);
textbox.Background = new SolidColorBrush(Colors.Red);
}
}
doNotFireTextBoxes.Remove(txtBoxName)
}
}

