Click here to Skip to main content
15,436,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a view model to modify an entity and i want update the source when the user press save button so I’ve set the UpdateSourceTrigger to explicit but now how can i update the source in MVVM pattern?

With explicit binding, this has always been expected to update the binding source using BindingExpression.

I have tried relay command with parameters also. But it all goes by assumption.

Advise me. Thanks in advance
Posted

Please write this code in event "OnPreviewKeyDown" of your .XAML window:

if (e.Key == Key.Enter)
{
var focusedElement = Keyboard.FocusedElement as FrameworkElement;

if (focusedElement is TextBox)
{
var expression = focusedElement.GetBindingExpression(TextBox.TextProperty);
if (expression != null) expression.UpdateSource();
}
}
 
Share this answer
 
Comments
Member 14634495 30-Mar-20 5:07am    
good answer. thank you bro
Bohdan Stupak 18-Apr-22 9:57am    
there's MVVM in question and no MVVM in answer
In cases like that, I usually have an editing view model, that makes a copy of all the properties that are editable, and I bind to those. The save button will copy the editable properties into the model's properties.

Say a model with properties "Name: and "Age" will have a view model with "EditableName" and "EditableAge."

If my view model was solely for the editing view, then I'd probably just use a view model that takes the model in its constructor, copies the properties into the vm's properties (ie: doesn't directly forward / write to model) and keep the names Name and Age in the view model.

Does that make sense?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900