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
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}" />
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.)