单击按钮时验证 ViewModel

发布于 2024-08-04 18:21:11 字数 94 浏览 5 评论 0原文

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

I have a ViewModel that implements IDataErrorInfo and a master-detail-view. How can I trigger the vaildation of the current ViewModel item when the user hits the save button in the detail view and not earlier?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2024-08-11 18:21:11

首先在您的虚拟机上添加一个标志,将其初始设置为 false。
在按钮命令代码中(假设您已将按钮绑定到虚拟机上的命令),在运行验证代码之前打开该标志。

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

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

Start by including a flag on your VM, set it initally to false.
In your Button command code (assuming you have bound your button to a command on your VM), turn on the flag before running your validation code.

In the "get" code in the IDataErrorInfo properties, only return a validation error if the flag is set to true, otherwise return an empty string.

Before switching the flag back to false raise a PropertyChangedEvent with an empty string as the property name, this will force the binding system to reevaluate all bindings in the current context, as well as check for errors against IDataErrorInfo.

说不完的你爱 2024-08-11 18:21:11

benPearce给出了很好的答案。

正如他指出的那样。

  1. this[columnName] 返回 null(即使数据无效),直到您
  2. 在“保存”命令中单击“保存”,您需要调用 OnPropertyChanged(null) 让 WPF 重新-评估绑定(并询问索引器)

该示例使用字典来代替标志来实现相同的结果。


视图

<TextBox Text="{Binding Surname, ValidatesOnDataErrors=True}" />

ViewModel

public string Surname { get; set; }

#region Validation
//http://blogs.msdn.com/b/bethmassi/archive/2008/06/27/displaying-data-validation-messages-in-wpf.aspx
Dictionary<string, string> validationErrors = new Dictionary<string,string>();

void Validate()
{
    validationErrors.Clear();
    if (srtring.IsNullOrWhitespace(Surname)) // Validate Surname 
    {
        validationErrors.Add("Surname", "Surname is mandatory.");
    }

    //http://stackoverflow.com/a/5210633/240835
    // Call OnPropertyChanged(null) to refresh all bindings and have WPF check the this[string columnName] indexer.
    OnPropertyChanged(null);
}

#region IDataErrorInfo Members
public string Error
{
    get 
    {
        if (validationErrors.Count > 0)
        {
            return "Errors found.";
        }
        return null;
    }
}

public string this[string columnName]
{
    get 
    {                
        if (validationErrors.ContainsKey(columnName))
        {
            return validationErrors[columnName];
        }
        return null;
    }
}

#endregion
#endregion
public void Save()
{
    Validate();
    if (validationErrors.Count == 0)
    {
        DoSave();
    }
}

benPearce has given a great answer.

As he pointed out.

  1. have this[columnName] return null (even if data is invalid) until you click "Save"
  2. within the Save command you need to call OnPropertyChanged(null) to let WPF re-evaluete the bindings (and interrogate the indexer)

Instead of using a flag this sample uses a Dictionary to achieve the same result.


In the View

<TextBox Text="{Binding Surname, ValidatesOnDataErrors=True}" />

In the ViewModel

public string Surname { get; set; }

#region Validation
//http://blogs.msdn.com/b/bethmassi/archive/2008/06/27/displaying-data-validation-messages-in-wpf.aspx
Dictionary<string, string> validationErrors = new Dictionary<string,string>();

void Validate()
{
    validationErrors.Clear();
    if (srtring.IsNullOrWhitespace(Surname)) // Validate Surname 
    {
        validationErrors.Add("Surname", "Surname is mandatory.");
    }

    //http://stackoverflow.com/a/5210633/240835
    // Call OnPropertyChanged(null) to refresh all bindings and have WPF check the this[string columnName] indexer.
    OnPropertyChanged(null);
}

#region IDataErrorInfo Members
public string Error
{
    get 
    {
        if (validationErrors.Count > 0)
        {
            return "Errors found.";
        }
        return null;
    }
}

public string this[string columnName]
{
    get 
    {                
        if (validationErrors.ContainsKey(columnName))
        {
            return validationErrors[columnName];
        }
        return null;
    }
}

#endregion
#endregion
public void Save()
{
    Validate();
    if (validationErrors.Count == 0)
    {
        DoSave();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文