在 ShowDialog (WPF MVVM) 中将 UpdateSourceTrigger 设置为显式
我
在示例中 看到了这个示例 - Binding.UpdateSourceTrigger Property UpdateSourceTrigger 设置为 Explicit,然后在视图代码中调用 TextBox 名称的 UpdateSource。
但是,如果我使用 MVVM dp,我不想为控件命名,并且源属性位于 VM 中而不是视图中,那么将控件绑定到 VM 属性并将 UpdateSourceTrigger 设置为显式的正确方法是什么?
我想这样做是因为在我的例子中它是 ShowDialog 窗口,并且我希望仅当用户单击“确定”时源才会更新
提前致谢!
I saw this example - Binding.UpdateSourceTrigger Property
in the example the UpdateSourceTrigger set to Explicit and then in the view code he call to UpdateSource of the TextBox name.
But if i use the MVVM dp i dont want to have names to my controls and source properties are in the VM and not in the view so what is the right way to bind controls to VM properties and set the UpdateSourceTrigger to explicit?
I want to do this because in my case its ShowDialog window and I want that the source will update only if the user click "ok"
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您真正使用 MVVM,那么您的“确定”按钮单击必须由某些
命令
处理。此命令必须来自您的ViewModel
。显式
绑定的属性必须再次来自您的ViewModel
。那么是什么阻止了你呢。Explicit
绑定,而是使用OneWay
绑定。OneWay
绑定的依赖项属性。ViewModel
中引发该属性的NotifyPropertyChanged
。例如,
假设我需要在单击“确定”按钮时将文本框的文本更新回我的模型。
因此,我有一个 EmployeeViewModel 类,其中包含 EmployeeName 属性。该属性有一个 getter 和一个 setter。 setter 会引发属性更改通知。视图模型还有另一个名为
SaveNameCommand
的ICommand
类型属性,它返回一个命令供我执行。EmployeeViewModel 是我的视图的数据上下文类型。 Myview 有一个绑定到
EmployeeName
的TextBox
(名为 x:Name="EmployeeNameTxBx")OneWay
和一个按钮OK< /代码>。我将
Button.Command
属性绑定到EmployeeViewModel.SaveNameCommand
属性,并将Button.CommandParameter
绑定到EmployeeNameTxBx.Text
属性。在我的
EmployeeViewModel
中,我有OnSaveNameCommandExecute(object param)
方法来执行我的SaveNameCommand
。在此执行此代码...
这样,只需单击“确定”按钮,即可将 TextBox 的文本更新回模型的 EmployeeName 属性。
编辑
查看下面的评论,我发现您正在尝试在 UI 上实现验证。现在这改变了一些事情。
仅当您的输入控件(例如文本框)是 TwoWay 绑定时,
IDataErrorInfo
和相关验证才有效。是的,这就是它的意图。所以现在你可能会问“如果我们使用 IDataErrorInfo,这是否意味着不允许无效数据传递到模型的整个概念在 MVVM 中是徒劳的”?其实不是!
请参阅 MVVM 不强制执行仅应返回有效数据的规则。它接受无效数据,这就是
IDataErrorInfo
的工作原理并引发错误通知。关键是 ViewModel 只是 View 的软拷贝,因此它可能脏。它应该确保这种肮脏不会提交到您的外部接口,例如服务或数据库。ViewModel 应该通过测试无效数据来限制此类无效数据流。如果我们启用了
TwoWay
绑定,这些数据就会出现。因此,考虑到您正在实现IDataErrorInfo
,那么您需要拥有 MVVM 中完全允许的TwoWay
绑定。方法 1:
如果我想在单击按钮时显式验证 UI 上的某些项目该怎么办?
为此,请使用延迟验证技巧。在您的 ViewModel 中有一个名为 isValidating 的标志。默认设置为 false。
在您的 IDataErrorInfo.this 属性中,通过检查 isValidating 标志来跳过验证...
然后在您的 OK 命令执行处理程序中,检查员工姓名,然后引发同一属性的属性更改通知事件...
这将触发仅当您单击“确定”时才进行验证。请记住,
EmployeeName
必须包含无效数据才能进行验证。方法 2:
如果我想在 MVVM 中显式更新没有 TwoWay 模式的绑定该怎么办?
那么您将必须使用
附加行为
。该行为将附加到“确定”按钮,并将接受需要刷新其绑定的所有项目的列表。ListMaker
是一个IMultiValueConverter
,它只是将值转换为列表...在您的
SpecialBindingBehavior
中有一个DependentControls
属性更改了处理程序...但我仍然建议您使用我之前的基于纯 MVVM **方法 1。
If you are using MVVM truely then your OK button click must be handled by some
Command
. This command must be coming from yourViewModel
. TheExpliticly
bound properties must be coming from yourViewModel
again. So whats stopping you.Explicit
binding but useOneWay
binding.OneWay
bound Dependency property.NotifyPropertyChanged
for that property from yourViewModel
.E.g.
Assume I need to update a TextBox's Text back into my model on OK button click.
So for that I have a
EmployeeViewModel
class that hasEmployeeName
property in it. The property is has a getter and a setter. The setter raises property changed notification. The view model also has another property of typeICommand
namedSaveNameCommand
that return a command for me to execute.EmployeeViewModel
is the data context type of my view. Myview has aTextBox
(named as x:Name="EmployeeNameTxBx")OneWay
bound to theEmployeeName
and a Button asOK
. I bindButton.Command
property toEmployeeViewModel.SaveNameCommand
property andButton.CommandParameter
is bound toEmployeeNameTxBx.Text
property.Inside my
EmployeeViewModel
I haveOnSaveNameCommandExecute(object param)
method to execute mySaveNameCommand
.In this perform this code...
This way ONLY OK button click, updates the TextBox's text back into
EmployeeName
property of the model.EDIT
Looking at your comments below, I see that you are trying to implement Validation on a UI. Now this changes things a little bit.
IDataErrorInfo
and related validation works ONLY IF your input controls (such as TextBoxes) are TwoWay bound. Yes thats how it is intended. So now you may ask "Does this mean the whole concept of NOT ALLOWING invalid data to pass to model is futile in MVVM if we use IDataErrorInfo"?Not actually!
See MVVM does not enforce a rule that ONLY valid data should come back. It accept invalid data and that is how
IDataErrorInfo
works and raises error notfications. The point is ViewModel is a mere softcopy of your View so it can be dirty. What it should make sure is that this dirtiness is not committed to your external interfaces such as services or data base.Such invalid data flow should be restricted by the
ViewModel
by testing the invalid data. And that data will come if we haveTwoWay
binding enabled. So considering that you are implementingIDataErrorInfo
then you need to haveTwoWay
bindings which is perfectly allowed in MVVM.Approach 1:
What if I wan to explicitly validate certain items on the UI on button click?
For this use a delayed validation trick. In your ViewModel have a flag called isValidating. Set it false by default.
In your
IDataErrorInfo.this
property skip the validation by checking isValidating flag...Then in your OK command executed handler, check employee name and then raise property change notification events for the same property ...
This triggers the validation ONLY when you click OK. Remember that
EmployeeName
will HAVE to contain invalid data for the validation to work.Approach 2:
What if I want to explicitly update bindings without TwoWay mode in MVVM?
Then you will have to use
Attached Behavior
. The behavior will attach to the OK button and will accept list of all items that need their bindings refreshed.The
ListMaker
is aIMultiValueConverter
that simply converts values into a list...In your
SpecialBindingBehavior
have aDependentControls
property changed handler...But I will still suggest you use my previous pure MVVM based **Approach 1.
这是一个老问题,但我仍然想为偶然发现这个问题的其他用户提供一种替代方法......
在我的视图模型中,我不会直接在 get/set Property 方法中公开模型属性。我对所有属性使用内部变量。然后我双向绑定所有属性。所以我可以像“通常”一样进行所有验证,因为只有内部变量发生了变化。在视图模型构造函数中,我将模型对象作为参数,并将内部变量设置为模型的值。现在,当我单击“保存”按钮(->“保存命令”在我的视图模型中触发)并且没有错误时,我将模型的所有属性设置为相应内部变量的值。如果我单击“取消/撤消”按钮(在视图模型中触发取消命令),我将内部变量设置为未触及模型的值(使用视图模型属性的设置器,以便 NotifyPropertyChanged被调用并且视图显示更改=旧值)。
另一种方法是在模型中实现 Memento-Support,因此在开始编辑之前,您可以在模型中调用一个函数来保存当前值,如果取消编辑,您可以调用一个函数来恢复这些值...这样您将在任何地方都有撤消/取消支持,而不仅仅是在一个视图模型中......
我已经在不同的项目中实现了这两种方法,并且都工作正常,这取决于项目的要求......
This is an old question but still I want to provide an alternative approach for other users who stumble upon this question...
In my viewmodels, I do not expose the model properties directly in the get/set Property methods. I use internal variables for all the properties. Then I bind all the properties two-way. So I can do all the validation as "usual" because only the internal variables are changed. In the view model constructor, I have the model object as parameter and I set the internal variables to the values of my model. Now when I click on the "Save" Button (-> Save Command fires in my view model fires) and there are no errors, I set all the properties of my model to the values of the correspondng internal variable. If I click on the "Canel/Undo"-Button (-> Cancel-Command in my view model fires), I set the internal variables to the values of my untouched model (using the setters of the view model properties so that NotifyPropertyChanged is called and the view shows the changes=old values).
Yet another approach would be to implement Memento-Support in the model, so before you start editing you call a function in the model to save the current values, and if you cancel editing you call a function to restore those values...that way you would have the undo/cancel support everywhere an not just in one view model...
I've implemented both methods in different projects and both work fine, it depends on the requirements of the project...