WPF MVVM 仅在用户确认后更新源属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21521569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:41:48  来源:igfitidea点击:

WPF MVVM Update source property only after user confirmation

c#wpfmvvmbinding

提问by Victor Mukherjee

I am new to wpf and I have this situation. Let's say that I have a Customermodel with FirstName, LastName, Telephoneand so on. If the details of an existing customer needs to be edited, the CustomerEditviewmodel is opened where it has a property of type Customer. The view has some textboxes bounded to CurrentCustomer.FirstName, CurrentCustomer.LastNameand 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?

我是 wpf 的新手,我遇到了这种情况。比方说,我有一个Customer模型FirstNameLastNameTelephone等等。如果需要编辑现有客户的详细信息,CustomerEdit则会打开具有 type 属性的视图模型Customer。该视图有一些绑定到 的文本框CurrentCustomer.FirstNameCurrentCustomer.LastName依此类推。现在,每当用户在这些文本框中提供输入时,bounded 属性都会更新。有一个按钮用于保存所做的更改。是否有某种方法可以仅在按下保存按钮时更新源属性,如果可能,以 MVVM 方式更新?

回答by Rohit Vats

By default TextDP's, UpdateSourceTriggervalue is LostFocus. Change it to Explicitand from save button click manually update the source by getting binding expressionand calling UpdateSource()on it.

默认TextDP 的UpdateSourceTrigger值为LostFocus. 通过获取绑定表达式并调用它Explicit,将其更改为保存按钮单击手动更新源UpdateSource()

XAML:

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:

在代码隐藏中使用此逻辑不会违反任何 MVVM 规则,但如果您不希望在代码隐藏中使用它。您可以通过这种方式实现:

Create an ICommandin your view model and bind to button command and in command parameter pass the text value of textBox. You can either use RelayCommandor DelegateCommandwhichever suits your needs. For DelegateCommand refer to this here.

ICommand在您的视图模型中创建一个并绑定到按钮命令并在命令参数中传递 textBox 的文本值。您可以使用RelayCommandDelegateCommand适合您的需要。对于 DelegateCommand,请参阅此处

<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.

并在 ViewModel 命令方法中设置文本框文本绑定到的实际值。

private void SaveMethod(object parameter)
{
   this.PropertyName = parameter.ToString();
}

回答by Mukesh Rawat

Apply UpdateSourceTrigger Explicit like

应用 UpdateSourceTrigger 显式像

<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"

“如果您有一个对话框或用户可编辑的表单,并且您想推迟源更新,直到用户完成编辑字段并单击“确定”,您可以将绑定的 UpdateSourceTrigger 值设置为 Explicit”

MVVM Style:

MVVM 风格:

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).

如果您想以 MVVM 样式执行此操作,并且您有多个文本框,那么我建议您使用转换器并在那里创建“客户”对象,您可以在 VM 中轻松地将其转换回来。处理您的 VM 中的 ICommand(将从您的按钮引发)并使用 Converter(您可以作为输入传递给转换器的文本框值)创建 CommandParameter。

回答by Mujahid Daud Khan

In MVVM scenario implement IEditableObjectinterface in View-modelas explained and recommend in this SO Answer

在 MVVM 场景IEditableObjectView-model,按照此 SO Answer 中的解释和推荐实现接口

IEditableObjectis a good interface anytime you want to be able to roll back changes.

IEditableObject是一个很好的界面,您可以随时回滚更改。