禁用触发TextChanged事件

10

我有一个文本框,当失去焦点时改变其中的文本,但这也会触发textChanged事件,我正在处理它,但我不想在这种情况下触发它,我该如何禁用它?

更新:

使用bool的想法很好,但我有几个文本框,我对所有文本框使用相同的事件,所以它并不能像我想要的那样完全工作。

现在它可以工作了!:

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;
   }

在编辑文本后,我成功地将 bool 改回了 true,现在它可以正常工作了,谢谢大家 :]


然后修改@Tigran的想法 private Dictionary<TextBox, bool> setFire = ... - dkozl
在我看来,不需要字典。像我的例子一样,一个字符串列表就足够了。 - Fabian Bigler
请将以下与编程相��的内容从英语翻译成中文。只返回翻译后的文本:不要将解决方案作为问题的一部分发布!如果有人回答了它,简单地“接受”就足够了。如果您没有得到令人满意的答案,但想出了自己的解决方案,请将其作为答案发布。 - walther
5个回答

13

只需删除事件处理程序,然后在完成所需操作后再添加它。

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;    
}

9
我能想到的最简单的方法是使用条件 bool 变量。当你要在 LostFocus 上设置文本时,将其设置为 true,并在 textChanged 事件处理程序中检查该 bool 变量是否为 true,如果是,则不进行任何操作。

3
我认为您也可以直接使用 UI 中内置的 "IsFocused" 布尔值。
private void mytextbox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textbox = sender as TextBox;
    if (!textbox.IsLoaded || !textbox.IsFocused)
        return;
     
                
    if(textbox.Text.ToString().Contains('.'))
    {
        textbox.Foreground = new SolidColorBrush(Colors.White);
        textbox.Background = new SolidColorBrush(Colors.Red);
    }  
}

这样你就不需要为每个输入都设置唯一的布尔值。祝您顺利。

1

我觉得..我们可以以最好且简单的方式来完成这件事情..!

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

0
如果您有多个文本框,只需使用一个字符串列表,其中存储状态为DoNotFire的文本框。

您更新的代码(还改进了其他部分):

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)
       }
   }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接