简体   繁体   English

TextBox TextChanged事件问题

[英]TextBox TextChanged Event Problems

I'm using a basic TextBox that is bound to an object. 我正在使用绑定到对象的基本TextBox。 Basically, what I want to do is call a method every time the text in the box is edited and the user de-selects the box or when the underlying bound data is edited. 基本上,我想做的是每次在框中的文本被编辑并且用户取消选择框时或在编辑基础绑定数据时都调用一个方法。 What I am using right now is the TextChanged event, but this has a few problems: 我现在使用的是TextChanged事件,但这有一些问题:

  1. It is called when the TextBox is first created, and I don't want this. 第一次创建TextBox时会调用它,我不希望这样。

  2. It is called every time a new character is added, and I only want it called when the underlying bound data is changed (which seems to be whenever focus shifts from the box). 每次添加新字符时都会调用它,而我只希望在底层绑定数据发生更改时调用它(这似乎是在焦点从框移开时)。

How can I accomplish this? 我该怎么做?

EDIT: I've tried several other TextBox properties like Get/LostFocus but they never seem to fire. 编辑:我尝试了其他几个文本框属性,如Get / LostFocus,但它们似乎从未触发过。

Also, I don't want to put this method call in the Setter of the Property, because the underlying data is something that is logically separate from the UI of this project and I don't want any method calls that relate to doing computations for the UI. 另外,我也不想将此方法调用放到Property的Setter中,因为底层数据在逻辑上与该项目的UI分离,并且我不希望任何与进行计算相关的方法调用用户界面。

The event LostFocus fires when the focus is shifted from the current element. 当焦点从当前元素LostFocus时,将触发事件LostFocus I tried it and its working fine. 我尝试了它,并且工作正常。

As jods says, the best way to bind your TextBox's Text to ViewModel's property. 正如jods所说,将TextBox的Text绑定到ViewModel的属性的最佳方法。 The Code are: 守则是:

View: 视图:

<TextBox x:Name="TextBox1" Text="{Binding Path=Text1,Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>

ViewModel: 视图模型:

    public string Text1
    {
        get { return _text1; }
        set
        {
            _text1 = value;
            RaisePropertyChanged("Text1");
        }
    }

View code behind: 查看后面的代码:

    private void ViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Text1")
        {
            //Call UI related method...
        }
    }

In this way, it satisfy your two conditions: 1. Every time when you edit TextBox and lose the focus, Setter of Text1 will be called and ViewModel will raise PropertyChanged event. 这样,它可以满足您的两个条件:1.每次编辑TextBox并失去焦点时,将调用Text1的Setter,并且ViewModel将引发PropertyChanged事件。 2. When underlying Text1 is changed. 2.基础Text1更改时。 Text1 will also raise the event so View can know it. Text1也将引发该事件,以便View可以知道它。

Also it can avoid your two concerns: 1. In the first time binding, only getter of Text1 is called. 它还可以避免您的两个问题:1.在第一次绑定中,仅调用Text1的getter。 No event is raised. 没有事件发生。 2. Setter of Text1 is only called after TextBox is lost focus. 2.仅在TextBox失去焦点后才调用Text1的Setter。

every time the text in the box is edited and the user de-selects the box 每次编辑框中的文本并且用户取消选择该框时

Hmmm AFAIK it's a standard behaviour of TextBox if you bind text like that: Text={Binding Property} 嗯,如果您这样绑定文本,则这是TextBox的标准行为: Text={Binding Property}

when the underlying bound data is edited 当底层绑定数据被编辑时

You can provide this functionality inside setter of your property. 您可以在属性的设置器中提供此功能。

Best design is to listen for changes in the underlying bound property. 最佳设计是侦听底层绑定属性的变化。 You can do that without changing the setter if you use a DependencyProperty or if your object implements INotifyPropertyChanged. 如果使用DependencyProperty或对象实现了INotifyPropertyChanged,则可以在不更改设置器的情况下进行操作。

When the underlying property changes (LostFocus by default, or each char at a time) is a binding option. 当基础属性发生更改时(默认情况下为LostFocus或一次每个字符)是一个绑定选项。

If you don't want to follow my advice of listenning for changes in your (view-)model, you could subscribe to GotFocus and LostFocus events. 如果您不想听从我的建议(侦听模型中的更改),可以订阅GotFocus和LostFocus事件。 Save the current value when you get focus, compare with current value when you lose it. 获取焦点时保存当前值,丢失时与当前值进行比较。 If it's different -> do what it is you want to do. 如果不同,请执行您想做的事情。

I am not sure what you are finally trying to achieve but I am going to take a guess at this. 我不确定您最终要达到的目标,但是我将对此做出猜测。 If you are following an MVVM pattern then, then it seems like you can achieve what you want by using the updateSourceTrigger property of the binding. 如果遵循的是MVVM模式,则似乎可以通过使用绑定的updateSourceTrigger属性来实现所需的功能。 If you are not using MVVM then you might what to take a look at using MVVM 如果您不使用MVVM,则可能要看一下MVVM

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

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