WPF:文本框未触发 onTextInput 事件 [英] WPF: Textbox not firing onTextInput event
问题描述
所以基本上,我有一堆文本框供用户填写.我有一个按钮,我想保持禁用状态,直到所有文本框都输入了文本.这是我正在使用的示例 XAML 文本框:
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");
}
当应该触发 TextInput 时,MessageBox 未显示.作为一个实验,我尝试在 PreviewTextInput 上触发 CheckTextBoxFilled(),然后它运行良好,这意味着无论出于何种原因,该函数都没有被调用.我还有一个由 PreviewTextInput 触发的验证功能,它可以正常工作.起初我认为 PreviewTextInput 可能会以某种方式干扰 TextInput,所以我将 PreviewTextInput 从 TextBox 中移除,但这并没有解决任何问题.我完全被为什么会发生这种情况弄糊涂了,所以任何帮助都将不胜感激.
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
事件处理程序未触发,因为 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
.
这篇关于WPF:文本框未触发 onTextInput 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!