WPF:文本框未触发 onTextInput 事件

发布于 2024-08-03 17:45:58 字数 1005 浏览 10 评论 0原文

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

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

这是我尝试触发的函数:

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

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

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

欢你一世 2024-08-10 17:45:58

TextInput 事件的处理程序不会被触发,因为 TextBox 正在处理该事件。您可以尝试使用 TextChanged 事件,因为实际上您只想知道何时从 TextBox 添加或删除字符。

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

音盲 2024-08-10 17:45:58
InitializeComponent();
textbox.AddHandler(TextBox.TextInputEvent, 
                   new TextCompositionEventHandler(TextBox_TextInput_1), 
                   true);
InitializeComponent();
textbox.AddHandler(TextBox.TextInputEvent, 
                   new TextCompositionEventHandler(TextBox_TextInput_1), 
                   true);
小嗷兮 2024-08-10 17:45:58

使用“PreviewTextInput”代替,它会起作用。

Use "PreviewTextInput" instead, it will work.

在巴黎塔顶看东京樱花 2024-08-10 17:45:58

创建一个从 TextBox 派生的新类。在新类中重写 OnTextInput 方法。您的 OnTextInput 方法将在 TextBox 获取它之前被调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文