简体   繁体   English

确定文本框在丢失的焦点事件中的先前值? WPF

[英]determine a textbox's previous value in its lost focused event? WPF

I have a textbox and have an onlostfocus event on it. 我有一个文本框,并有一个onlostfocus事件。

Inside the lostfocus method, is there a way I can determine if the user has actually changed the value in it? 在lostfocus方法中,有没有办法可以确定用户是否实际更改了其中的值? ie how do i get hold of any previous value in it? 即我如何获得其中的任何先前价值?

Thanks 谢谢

As with just about everything else in WPF, this is easier if you use data binding. 与WPF中的其他所有内容一样,如果使用数据绑定,这将更容易。

Bind the text box to a class property. 将文本框绑定到类属性。 By default, bindings update the source when the bound control loses focus, so you don't have to muck around with the LostFocus event. 默认情况下,绑定控件失去焦点时绑定会更新源,因此您不必使用LostFocus事件。 You then have access to both the new value and the value that the user entered in the property setter. 然后,您可以访问新值和用户在属性设置器中输入的值。

In the XAML it looks like this: 在XAML中它看起来像这样:

<TextBox Text="{Binding MyProperty, Mode=TwoWay}"/>

In the class it looks like this: 在课堂上它看起来像这样:

private string _MyProperty;

public string MyProperty
{
   get { return _MyProperty; }
   set
   {
      // at this point, value contains what the user just typed, and 
      // _MyProperty contains the property's previous value.
      if (value != _MyProperty)
      {
         _MyProperty = value;
         // assuming you've implemented INotifyPropertyChanged in the usual way...
         OnPropertyChanged("MyProperty"); 
      }
   }

What comes to mind for me is a two stage approach. 我想到的是一个两阶段的方法。 Handle the TextChanged event on the textbox and flag it. 处理文本框上的TextChanged事件并标记它。 Then when the textbox OnLostFocus occurs you can simply check your flag to see if the text has been changed. 然后,当文本框OnLostFocus发生时,您只需检查您的标志以查看文本是否已更改。

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;
        }              
    }
}

Store the original value somewhere. 将原始值存储在某处。 You could write a common component to store the value when it gets focus and compare the value when it loses focus. 您可以编写一个公共组件来在焦点获得焦点时存储该值,并在失去焦点时比较该值。 I've done this in ASP.NET and it works quite well. 我在ASP.NET中做到了这一点并且运行良好。

Another way to solve this by databinding: Bind the TextBox.Text to the property, that holds the inital value, but use a binding with UpdateSourceTrigger=Explicit Then, when the textbox loses focus, you can check the binding if source and target values differ, using this code snippet and evaluating the resulting BindingExpression: BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty); 通过数据绑定解决此问题的另一种方法:将TextBox.Text绑定到保存初始值的属性,但使用与UpdateSourceTrigger=Explicit的绑定然后,当文本框失去焦点时,如果源和目标值不同,则可以检查绑定,使用此代码片段并评估生成的BindingExpression: BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty); Some more code can be found here: http://bea.stollnitz.com/blog/?p=41 可以在这里找到更多代码: http//bea.stollnitz.com/blog/?p = 41

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM