TextBox更改事件 [英] TextBox change Event

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

问题描述

您好,

我正在构建一个wpf窗口应用程序,在这里我设计一个动态Datagrid并在该网格中我提供了一个文本框,当用户输入文本时,应该进行一些计算在网格...但我的问题是,当用户试图在文本框中输入100并输入1表示事件正在触发....所以对于3位数,它将触发3次...所以请在将完整的数据输入文本框后帮助我调用一次事件....我对模糊事件的处理也没有问题...请帮我提供代码...这是我的处理程序写的.. 。



DataTemplate textBoxTemplate = new DataTemplate();

FrameworkElementFactory textboxElement = new FrameworkElementFactory(typeof(TextBox));

绑定textboxBinding = new绑定(LineItemUnits);

绑定txtLineItemID = new绑定(LineItemID);

textboxE lement.AddHandler(TextBox.TextChangedEvent,new TextChangedEventHandler(txtUnitCount_Changed));

textboxElement.SetBinding(TextBox.TagProperty,txtLineItemID);

textBoxTemplate.VisualTree = textboxElement;

templateColumn.CellTemplate = textBoxTemplate;

grdCharges.Columns.Add(templateColumn);



textboxElement.AddHandler(TextBox .TextChangedEvent,

newTextChangedEventHandler(txtUnitCount_Changed));





Hello,
I am builing a wpf window application, Here i am desiging a dynamic Datagrid and in that grid i am providing a text box, when the user enters the text some calculations should be done in the grid... But my problem is that whenn the user tries to enter "100" in the text box and enter "1" means the event is firing.... so for 3 digits it is firing 3 times... so please help me to call the event only once after entering complete data into text box .... I have no problem in handling on blur events also... Please help me with providing code... here is my handler written...

DataTemplate textBoxTemplate = new DataTemplate();
FrameworkElementFactory textboxElement = new FrameworkElementFactory(typeof(TextBox));
Binding textboxBinding = new Binding("LineItemUnits");
Binding txtLineItemID = new Binding("LineItemID");
textboxElement.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(txtUnitCount_Changed));
textboxElement.SetBinding(TextBox.TagProperty, txtLineItemID);
textBoxTemplate.VisualTree = textboxElement;
templateColumn.CellTemplate = textBoxTemplate;
grdCharges.Columns.Add(templateColumn);

textboxElement.AddHandler(TextBox.TextChangedEvent,
newTextChangedEventHandler(txtUnitCount_Changed));


private void txtUnitCount_Changed(object Sender, TextChangedEventArgs e)
       {
           }

推荐答案

只有一次?我有一篇关于这个问题的完整CodeProject文章:希望你在这里......只有一次 [ ^ ]。



请注意,您不必使用我的高度通用方法,这种方法主要是为了证明上下文敏感条件技术的可能性以及好奇心,即使我的技术不仅具有高度通用性,而且实际上也非常实用。更深入地理解一些编程基础知识更为重要。但是你可以在一开始就使用我概述的基本技术。理解是你真正需要的。







是的,你是对的,你的问题是完全不同的,问题是:你没有正确理解数据输入和相关事件的概念。



这是事情:当用户输入(或甚至删除)单个字符,这是事件,但是当用户决定正确编辑所有字符时,它不是一个事件,因为这一切都在用户的脑海中,无处可去。



这意味着你的整个概念都是错误的。您需要要求用户实际确认输出已完成。例如,按Enter(您可以检测到处理 KeyDown KeyUp 事件,移动键盘焦点(您可以检测处理 LostFocus 事件):

http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown(v = vs.110)的.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.uielement.keyup(v = vs.110).aspx [ ^ ] ,

http: //msdn.micros oft.com/en-us/library/system.windows.uielement.lostfocus(v=vs.110).aspx [ ^ ]。



或者,如果您使用类似 MaskedTextBox 的控件,您可以检测到所有位置都已填充,例如:

< a href =http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox> http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox [ ^ ]。



然而,我不要认为这两种肯定有效的方法是最好的。我认为最好的选择是不要因为这种不确定的目的而处理这些事件。相反,我建议在即将使用输入数据的位置验证所有输入。然后,您可以清理数据,并且,如果数据不可满足,则关注违规输入控件并向用户提供不符合要求的说明。



这种方法虽然简单,但更灵活。我将解释原因:如果在一个控件中输入的数据的有效性取决于某些其他控件的状态,该怎么办?这种复杂情况很常见。因此,验证和卫生一次输入的所有数据都是更普遍的方法。



-SA
Only once? I have a whole CodeProject article on this problem: Wish You Were Here… Only Once[^].

Note that you don't have to use my highly universal approach, which is created mostly to demonstrate the possibility of context-sensitive condition techniques and also for curiosity, even though my technique is not only highly universal but also quite usable practically. It's more important for deeper understanding of some fundamentals of programming. But you can use the elementary technique I overview in the very beginning. Understanding is what you actually need the most.



Yes, you are right, your problem is quite different, and the problem is: you don't properly understand the concept of data input and related events.

Here is the thing: when a user enters (or even deletes) a single character, this is the event, but when the user decides that all characters are properly edited, it is not an event, because this is all in the user's head, nowhere.

It means that your whole concept is wrong. You need to require the user to actually confirm that the output is done. For example, press Enter (which you can detect handling KeyDown or KeyUp event, moves the keyboard focus (which you can detect handling LostFocus event):
http://msdn.microsoft.com/en-us/library/system.windows.uielement.keydown(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.keyup(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.lostfocus(v=vs.110).aspx[^].

Alternatively, you could detect that all positions are filled if you use something like the MaskedTextBox control, such as this one:
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox[^].

However, I don't think that these two approaches, which are certainly working, are the best. I think the best option would be not handling this events for such an uncertain purpose at all. Instead, I would recommend to validate all input at the very point where the input data can are about to be used. Then you can sanitize data, and, if data is unsatisfiable, focus the "offending" input control and provide the user with explanation what requirements are not met.

This approach, despite its simplicity, is more flexible. I'll explain why: what if validity of data entered in one control depends on the states of some other controls? Such complication is pretty usual. So, validation and sanitation all of the entered data at once is more universal approach.

—SA


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

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