WPF RaisePropertyChanged 事件失去焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17457984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:08:29  来源:igfitidea点击:

WPF RaisePropertyChanged event on lost focus

c#wpfmvvm

提问by guilhermecgs

I have a C# WPF MVVM application that works fine.

我有一个运行良好的 C# WPF MVVM 应用程序。

The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control, the view->viewmodel event is never fired because the textbox hasn't lost focus. Correct me if I am wrong, but I think the RaisePropertyChanged is only fired on LostFocus (or OnBlur, or any similar event).

唯一的问题是当我修改文本框并单击菜单时。如果我在不单击另一个控件的情况下执行此操作,则永远不会触发 view->viewmodel 事件,因为文本框没有失去焦点。如果我错了,请纠正我,但我认为 RaisePropertyChanged 仅在 LostFocus(或 OnBlur 或任何类似事件)上触发。

So, clicking on the menu save button right after editing the textbox causes the viewmodel to save the data using old values.

因此,在编辑文本框后立即单击菜单保存按钮会导致视图模型使用旧值保存数据。

So, resuming:

所以,继续:

This sequence works fine:

这个序列工作正常:

  1. Edit the text box
  2. Click on another control
  3. RaisePropertyChanged is fired, the viewmodel is updated
  4. Click on save button on the menu
  5. Data Saved with correct values
  1. 编辑文本框
  2. 单击另一个控件
  3. RaisePropertyChanged 被触发,视图模型被更新
  4. 点击菜单上的保存按钮
  5. 使用正确值保存的数据

This sequence gives me an error:

这个序列给了我一个错误:

  1. Edit the text box
  2. Click on save button on the menu
  3. Data Saved with correct values
  1. 编辑文本框
  2. 点击菜单上的保存按钮
  3. 使用正确值保存的数据

How to solve this?

如何解决这个问题?

回答by Michael Gunter

This is a common gotcha with TextBoxes in both WPF and WinForms. You can get around this by instructing the binding system to update the VM with every change to the TextBox instead of when it loses focus. To do this, set the UpdateSourceTriggerof the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChangedevent for its Textproperty.

这是 WPF 和 WinForms 中 TextBox 的常见问题。您可以通过指示绑定系统在每次更改 TextBox 时更新 VM 而不是在它失去焦点时更新 VM 来解决此问题。为此,请将UpdateSourceTrigger绑定的设置为PropertyChanged。每当 TextBoxPropertyChanged为其Text属性引发事件时,这都会写回 VM 。

<TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

回答by McGarnagle

For the TextBox.Textdependency property, its default UpdateSourceTriggeris LostFocus(ie, your view model property gets updated when the control loses focus). To make the property update immediately whenever text is entered, set UpdateSourceTrigger=PropertyChanged. (See the link above for more info -- it actually covers your example specifically.)

对于TextBox.Text依赖属性,它的默认值UpdateSourceTriggerLostFocus(即,当控件失去焦点时,您的视图模型属性会更新)。要在输入文本时立即更新属性,请设置UpdateSourceTrigger=PropertyChanged。(有关更多信息,请参阅上面的链接 - 它实际上专门涵盖了您的示例。)