禁用触发TextChanged事件

时间:2013-06-26 15:17:25

标签: c# wpf textbox textchanged lostfocus

我有文本框,当lostFocus被触发时我正在更改其中的文字,但这也会触发我正在处理的textChanged事件,但我不想在这种情况下被解雇,我怎么能在这里禁用它?

UPDATE:

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,这样就可以了。[/ p>

4 个答案:

答案 0 :(得分:9)

我能想到的最简单的方法是使用conditnional bool变量。 当您要在LostFocus上设置文本时,将其设置为true并在textChanged内部事件处理程序中检查bool变量是否为true,请勿执行此操作什么都没有。

答案 1 :(得分:9)

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

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

答案 2 :(得分: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;
        }

答案 3 :(得分: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)
       }
   }