UpdateSourceTrigger为显式MVVM [英] UpdateSourceTrigger to explicit MVVM
本文介绍了UpdateSourceTrigger为显式MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个修改实体的视图模型,我想在用户按下保存按钮时更新源,所以我设置了UpdateSourceTrigger显式,但现在我如何更新MVVM模式中的源?
使用显式绑定,一直期望使用BindingExpression更新绑定源。
我也试过带参数的继电器命令。但这一切都是基于假设。
告诉我。在此先感谢
解决方案
在这种情况下,我通常会有一个编辑视图模型,它会复制所有可编辑的属性,然后绑定到那些。保存按钮会将可编辑属性复制到模型的属性中。
假设具有属性的模型名称:和年龄将具有带EditableName的视图模型和EditableAge。
如果我的视图模型仅用于编辑视图,那么我可能只是使用一个视图模型,在其构造函数中获取模型,将属性复制到vm的属性中(即:不直接转发/写入模型)并在视图模型中保留名称Name和Age。
这样做有意义吗?
请在.XAML窗口的事件OnPreviewKeyDown中写下此代码:
if(e.Key == Key.Enter)
{
var focusedElement = Keyboard.FocusedElement as FrameworkElement;
if(focusedElement是TextBox)
{
var expression = focusedElement.GetB indingExpression(TextBox.TextProperty);
if(表达式!= null)expression.UpdateSource();
}
}
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
解决方案
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?
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();
}
}
这篇关于UpdateSourceTrigger为显式MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文