本文介绍了在丢失的焦点事件中确定文本框的先前值?WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框,上面有一个 onlostfocus 事件.
I have a textbox and have an onlostfocus event on it.
在 lostfocus 方法中,有没有一种方法可以确定用户是否实际更改了其中的值?即我如何获得其中的任何先前值?
Inside the lostfocus method, is there a way I can determine if the user has actually changed the value in it?i.e how do i get hold of any previous value in it?
谢谢
推荐答案
我想到的是两阶段方法.处理文本框上的 TextChanged
事件并标记它.然后当文本框 OnLostFocus
出现时,您可以简单地检查您的标志以查看文本是否已更改.
What comes to mind for me is a two stage approach. Handle the TextChanged
event on the textbox and flag it. Then when the textbox OnLostFocus
occurs you can simply check your flag to see if the text has been changed.
这是关于如何处理跟踪的代码片段.
Here is a code snippet on how you could handle the tracking.
public class MyView
{
private bool _textChanged = false;
private String _oldValue = String.Empty;
TextChanged( ... )
{
// The user modifed the text, set our flag
_textChanged = true;
}
OnLostFocus( ... )
{
// Has the text changed?
if( _textChanged )
{
// Do work with _oldValue and the
// current value of the textbox
// Finished work save the new value as old
_oldValue = myTextBox.Text;
// Reset changed flag
_textChanged = false;
}
}
}
这篇关于在丢失的焦点事件中确定文本框的先前值?WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!