wpf - 单击按钮时验证 ViewModel

标签 wpf validation mvvm idataerrorinfo

我有一个实现 IDataErrorInfo 和主细节 View 的 ViewModel。当用户点击详细 View 中的保存按钮而不是更早时,如何触发当前 ViewModel 项的验证?

最佳答案

首先在您的 VM 上包含一个标志,将其初始设置为 false。
在您的 Button 命令代码中(假设您已将按钮绑定(bind)到 VM 上的命令),在运行验证代码之前打开标志。

在 IDataErrorInfo 属性中的“get”代码中,如果标志设置为 true,则仅返回验证错误,否则返回空字符串。

在将标志切换回 false 之前,引发一个使用空字符串作为属性名称的 PropertyChangedEvent,这将强制绑定(bind)系统重新评估当前上下文中的所有绑定(bind),并针对 IDataErrorInfo 检查错误。

关于wpf - 单击按钮时验证 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1383297/

相关文章:

c# - DataGrid - 新数据到达时重置网格选择

c# - 绕过自动生成文件的自定义 App 类

WPF 数据网格 : set focus on a specific cell after a row validation error

validation - primefaces tabView 在选项卡更改时跳过表单验证

.net - 相邻复选框的验证逻辑

mvvm - Xamarin Forms 和 TabGestureRecognizer 不会使用 Command 触发

c# - 全局样式不起作用

WPF DataBound ListBox 添加动画但不滚动

c# - 在 UserControl 中绑定(bind) Validation.ErrorTemplate

php - 如何在mysqli_query和mysql_query中设置准备安全性声明?