I belive I saw this problem in WinForms also with the text changed event but I maybe mistaken. Regardless I want to know how (or if it can be) solved in WPF. Note I am using an MVVM pattern.
Say I have a TextBox in the view bound to a value 'Value' in its view model.
As the end user types or changes the text code is not fired. It is only when the mouse goes to another component (e.g. textbox or listbox.. really anyting) that the value is updated in the viewModel.
This can of course create problems. Say this 'Value' is saved to some file when the user presses 'Ctrl+S'. The value has not yet grabbed what is in the textbox so it is the old value. The user then thinks they saved new data but really did nothing but save old over old.
So How does one handle this without telling his end users [CLICK HERE] before saving?
EDIT (Addid code samples)
The main view is bound as such in the App.xaml.cs
<pre lang="cs">private void OnStartup(object sender, StartupEventArgs e)
{
MainWindow view = new MainWindow();
view.DataContext = new MainViewModel();
view.Show();
}
All others are bound in a resource that is pulled in by the main view. Their binding is as such,
<DataTemplate DataType="{x:Type viewModels:Example_vm}">
<views:Example_v/>
</DataTemplate>
The object that has the Textbox is as follows:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=Name}"/>
<TextBox Grid.Column="1" Text="{Binding Path=Value, Mode=TwoWay}"/>
</Grid>
And its bounded code is:
#region Value
public string Value
{
get { return _settingNode.Attribute(Setting_Properties.Value).Value; }
set
{
if (_settingNode.Attribute(Setting_Properties.Value).Value == value)
return;
_settingNode.Attribute(Setting_Properties.Value).SetValue(value);
this.RaisePropertyChanged("Value");
this.RaisePropertyChanged("Node");
}
}
#endregion //Value
<pre lang="cs">#region Name
public string Name
{
get { return _settingNode.Attribute(Setting_Properties.Name).Value; }
set
{
if (_settingNode.Attribute(Setting_Properties.Name).Value == value)
return;
_settingNode.Attribute(Setting_Properties.Name).SetValue(value);
this.RaisePropertyChanged("Name");
this.RaisePropertyChanged("Node");
}
}
#endregion //Name
_settingNode is an XElement that contains the data needed.