WPF:文本框不触发 onTextInput 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1374710/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:57:21  来源:igfitidea点击:

WPF: Textbox not firing onTextInput event

wpfeventstextboxtextinput

提问by Kay Ell

So basically, I have a bunch of TextBoxes that the user gets to fill out. I've got a button that I want to keep disabled until all the TextBoxes have had text entered in them. Here is a sample XAML TextBox that I'm using:

所以基本上,我有一堆用户可以填写的文本框。我有一个按钮,我想一直禁用它,直到所有的 TextBox 都输入了文本。这是我正在使用的示例 XAML 文本框:

<TextBox Name="DelayedRecallScore" TextInput="CheckTextBoxFilled" Width="24" />

And here is the function that I'm trying to trigger:

这是我试图触发的功能:

  //Disables the OK button until all score textboxes have content
    private void CheckTextBoxFilled(object sender, RoutedEventArgs e)
    {
        /*
        foreach (TextBox scorebox in TextBoxList)
        {
            if (string.IsNullOrEmpty(scorebox.Text))
            {
                Ok_Button.IsEnabled = false;
                return;
            }
        }
        Ok_Button.IsEnabled = true;
         */
        MessageBox.Show("THIS MAKES NO SENSE");
    }

The MessageBox is not showing up when TextInput should be getting triggered. As an experiment I tried triggering CheckTextBoxFilled() on PreviewTextInput, and it worked fine then, meaning that for whatever reason, the function just isn't getting called. I also have a validation function that is triggered by PreviewTextInput, which works as it should. At first I thought PreviewTextInput might somehow be interfering with TextInput, so I took PreviewTextInput off the TextBox, but that hasn't managed to fix anything. I'm completely befuddled by why this might happen, so any help would be appreciated.

当 TextInput 应该被触发时,MessageBox 没有出现。作为一个实验,我尝试在 PreviewTextInput 上触发 CheckTextBoxFilled(),然后它运行良好,这意味着无论出于何种原因,该函数都没有被调用。我还有一个由 PreviewTextInput 触发的验证函数,它可以正常工作。起初我认为 PreviewTextInput 可能会以某种方式干扰 TextInput,所以我从 TextBox 中取出了 PreviewTextInput,但这并没有解决任何问题。我完全不明白为什么会发生这种情况,所以任何帮助将不胜感激。

回答by Andy

Your handler for the TextInputevent is not fired because the TextBoxis handling the event. You could try using the TextChangedevent instead, since really you just want to know when characters were added or removed from the TextBox.

您的TextInput事件处理程序未触发,因为TextBox正在处理该事件。您可以尝试改用TextChanged事件,因为实际上您只想知道何时从TextBox.

回答by Lavesh

InitializeComponent();
textbox.AddHandler(TextBox.TextInputEvent, 
                   new TextCompositionEventHandler(TextBox_TextInput_1), 
                   true);

回答by Rankit

Use "PreviewTextInput" instead, it will work.

改用“PreviewTextInput”,它将起作用。

回答by user3801831

Create a new class derived from TextBox. In the new class override the OnTextInput method. Your OnTextInput method will get called before the TextBox gets it.

创建一个派生自 TextBox 的新类。在新类中覆盖 OnTextInput 方法。您的 OnTextInput 方法将在 TextBox 获取之前被调用。