WPF RaisePropertyChanged event on lost focus

后端 未结 2 1435
日久生厌
日久生厌 2021-01-05 15:51

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

The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control

相关标签:
2条回答
  • 2021-01-05 16:32

    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 UpdateSourceTrigger of the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChanged event for its Text property.

    <TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    
    0 讨论(0)
  • 2021-01-05 16:39

    For the TextBox.Text dependency property, its default UpdateSourceTrigger is 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.)

    0 讨论(0)
提交回复
热议问题