禁用触发TextChanged事件 [英] Disable firing TextChanged event

查看:177
本文介绍了禁用触发TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本,我改变了文本里面,当引发LostFocus 被激发,但也激发了框TextChanged 事件,我处理,但我不希望它在这一种情况下被解雇,我怎么能在这里禁用它?

I have textbox and I'm changing the text inside it when lostFocus is fired but that also fires up the textChanged event, which I'm handling but I don't want it to be fired in this one case, how can I disable it here?

布尔的想法是好的,但我有几个文本框,我使用相同的事件所有的人,所以它不完全工作,我想我想要的方式。

The idea with bool is 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.

现在它的工作!

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



我设法把布尔真正编辑文本后,和它的作品所以THX的家伙:]

I managed to put the bool back on true after editing the text and it works so thx guys :]

推荐答案

这是我能想到的Simpliest的方法是使用conditnional 布尔变量。
当你要设置引发LostFocus 文本设置为真正并在框TextChanged 事件处理程序检查如果布尔变量真正,什么都不干。

Simpliest way that I can think of is using conditnional bool variable. When you are going to set the text on LostFocus set it to true and inside textChanged event handler check if that bool variable is true, do not do nothing.

这篇关于禁用触发TextChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆