wpf 禁用激发TextChanged事件

6rvt4ljy  于 2022-11-18  发布在  其他
关注(0)|答案(5)|浏览(140)

我有一个textbox,当lostFocus被触发时,我正在更改其中的文本,但这也触发了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后,编辑文本和它的工作,所以thx家伙:]

o2rvlv0m

o2rvlv0m1#

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

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

ozxc1zmp2#

我能想到的最简单的方法是使用条件bool变量。当你要设置LostFocus上的文本时,将其设置为true,并在textChanged事件处理程序中检查bool变量是否为true,不要什么都不做。

ecbunoof

ecbunoof3#

我觉得..我们可以用最好的方法和简单的方法来做这件事..!

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

pvcm50d14#

如果你有多个文本框,只需要使用一个字符串列表来存储状态为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)
       }
   }
7ivaypg9

7ivaypg95#

我认为您也可以简单地使用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);
    }  
}

这样,您就不需要为每个输入使用唯一的bool。
高温

相关问题