WPF:文本框未触发 onTextInput 事件
基本上,我有一堆用户可以填写的文本框。我有一个按钮,我想保持禁用状态,直到所有文本框都输入了文本。这是我正在使用的示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TextInput
事件的处理程序不会被触发,因为TextBox
正在处理该事件。您可以尝试使用 TextChanged 事件,因为实际上您只想知道何时从TextBox
添加或删除字符。Your handler for the
TextInput
event is not fired because theTextBox
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 theTextBox
.使用“PreviewTextInput”代替,它会起作用。
Use "PreviewTextInput" instead, it will work.
创建一个从 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.