I am new to wpf and I have this situation. Let's say that I have a Customer
model with FirstName
, LastName
, Telephone
and so on. If the details of an existing customer needs to be edited, the CustomerEdit
viewmodel is opened where it has a property of type Customer
. The view has some textboxes bounded to CurrentCustomer.FirstName
, CurrentCustomer.LastName
and so on. Now whenever the user provided an input in those textboxes, the bounded property gets updated. There is a button for saving the changes done. Can there be some way to update source properties only when the save button is pressed, and if possible, in an MVVM way?

- 10,487
- 16
- 54
- 97
3 Answers
By default Text
DP's, UpdateSourceTrigger
value is LostFocus
. Change it to Explicit
and from save button click manually update the source by getting binding expression and calling UpdateSource()
on it.
XAML:
<TextBox x:Name="myTextBox"
Text="{Binding PropertyName, UpdateSourceTrigger=Explicit}"/>
<Button Click="btnSave_Click"/>
Code behind:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
Having this logic in code behind is not violating any MVVM rule but still if you don't want it in code behind. You can achieve this way:
Create an ICommand
in your view model and bind to button command and in command parameter pass the text value of textBox. You can either use RelayCommand
or DelegateCommand
whichever suits your needs. For DelegateCommand refer to this here.
<TextBox x:Name="myTextBox"
Text="{Binding PropertyName, UpdateSourceTrigger=Explicit}"/>
<Button Command="{Binding SaveCommand}"
CommandParameter="{Binding Text, ElementName=myTextBox}"/>
and in ViewModel command method set the actual value to which your textBox text is binded with.
private void SaveMethod(object parameter)
{
this.PropertyName = parameter.ToString();
}

- 79,502
- 12
- 161
- 185
-
i have seen this explicit way. but the thing I am looking for is how to update source after button pressed in an mvvm way. is there such an way? Perhaps I should edit my question. – Victor Mukherjee Feb 03 '14 at 07:20
-
1This way you are `not violating any MVVM rule`. There is lot of misconception that `MVVM means No code behind`. As per MVVM, your code behind should not have any business logic, but it's completely fineto have UI logic in there. (That's what is code behind for). – Rohit Vats Feb 03 '14 at 07:22
-
1@Victor - Updated with another approach without code behind. – Rohit Vats Feb 03 '14 at 07:27
Apply UpdateSourceTrigger Explicit like
<TextBox Name="itemNameTextBox"
Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />
MSDN:
"If you have a dialog or a user-editable form and you want to defer source updates until the user is finished editing the fields and clicks "OK", you can set the UpdateSourceTrigger value of your bindings to Explicit"
MVVM Style:
If you want to do it in MVVM Style and if you have multiple textboxes then I suggest you to use a converter and create "Customer" object at there, which you can cast back easily in your VM. Handle the ICommand in your VM (Which will raised from your Button) and create CommandParameter by using Converter (textboxes values you can pass as an input to your converter).

- 2,047
- 16
- 30
-
1Here's a detailed worked example: http://www.shujaat.net/2011/01/updatesourcetrigger-explicit-for-mvvm.html – kmote Dec 26 '16 at 06:39
In MVVM scenario implement IEditableObject
interface in View-model
as explained and recommend in this SO Answer
IEditableObject
is a good interface anytime you want to be able to roll back changes.

- 1
- 1

- 1,983
- 1
- 14
- 23